您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

关于MacOS开发-NSParagraphStyle

2026/1/13 0:24:56发布19次查看
简介
使用方法
计算段落高度
相关属性
alignment - 对齐方式 firstlineheadindent - 首行缩进 headindent - 缩进 tailindent - 尾部缩进 linebreakmode - 断行方式 maximumlineheight - 最大行高 minimumlineheight - 最低行高 linespacing - 行距 paragraphspacing -段距 paragraphspacingbefore - 段首空间 basewritingdirection - 句子方向 lineheightmultiple - 可变行高乘因数 hyphenationfactor - 连字符属性
简介nsparagraphstyleattributename 用于设置 nsattributedstring,和 nsfontattributename 类似。
不同的是:nsparagraphstyleattributename 是设置段落风格,nsfontattributename是设置字体。
使用方法- (void)drawrect:(nsrect)dirtyrect { [super drawrect:dirtyrect]; nsmutableparagraphstyle *textstyle = [[nsmutableparagraphstyle alloc] init]; [textstyle setalignment:nstextalignmentcenter]; [textstyle setlinebreakmode:nslinebreakbytruncatingtail]; nsdictionary *attributes = @{nsfontattributename: [nsfont systemfontofsize:18], nsforegroundcolorattributename: [nscolor whitecolor], nsparagraphstyleattributename: textstyle}; float w = self.frame.size.width; float h = self.frame.size.height; float str_h = 20; nsattributedstring *attributedtext4 = [[nsattributedstring alloc] initwithstring:@"这是一个 string" attributes:attributes]; }
计算段落高度 - (void)countheight{ nsstring *originstr = @"ashdhsaksah 和就会撒大声地哈萨克的"; nsmutableattributedstring *attristr = [[nsmutableattributedstring alloc] initwithstring:originstr]; nsfont *descfont = [nsfont systemfontofsize:16]; nsmutableparagraphstyle *descstyle = [[nsmutableparagraphstyle alloc]init]; [descstyle setlinespacing:1];//行间距 cgfloat destinw = 100; cgsize descsize = [originstr boundingrectwithsize:cgsizemake(destinw, maxfloat) options:nsstringdrawinguseslinefragmentorigin attributes:@{nsfontattributename:descfont, nsparagraphstyleattributename :descstyle} context:nil].size; }
相关属性nsmutableparagraphstyle与nsparagraphstyle包括一下属性
alignment - 对齐方式 firstlineheadindent - 首行缩进 headindent - 缩进 tailindent - 尾部缩进 linebreakmode - 断行方式 maximumlineheight - 最大行高 minimumlineheight - 最低行高 linespacing - 行距 paragraphspacing -段距 paragraphspacingbefore - 段首空间 basewritingdirection - 句子方向 lineheightmultiple - 可变行高,乘因数。 hyphenationfactor - 连字符属性
(void)setparagraphstyle{ nsmutableparagraphstyle *paragraphstyle = [nsmutableparagraphstyle new]; paragraphstyle.linespacing = 20.;// 行间距 paragraphstyle.lineheightmultiple = 1.5;// 行高倍数(1.5倍行高) paragraphstyle.firstlineheadindent = 30.0f;//首行缩进 paragraphstyle.minimumlineheight = 10;//最低行高 paragraphstyle.maximumlineheight = 20;//最大行高(会影响字体) paragraphstyle.alignment = nstextalignmentleft;// 对齐方式 paragraphstyle.defaulttabinterval = 144;// 默认tab 宽度 paragraphstyle.headindent = 20;// 起始 x位置 paragraphstyle.tailindent = 320;// 结束 x位置(不是右边间距,与inset 不一样) paragraphstyle.paragraphspacing = 44.;// 段落间距 paragraphstyle.paragraphspacingbefore = 44.;// 段落头部空白(实测与上边的没差啊?) paragraphstyle.linebreakmode = nslinebreakbywordwrapping;// 分割模式 /* nslinebreakbywordwrapping = 0, // wrap at word boundaries, default nslinebreakbycharwrapping, // wrap at character boundaries nslinebreakbyclipping, // simply clip nslinebreakbytruncatinghead, // truncate at head of line: “…wxyz” nslinebreakbytruncatingtail, // truncate at tail of line: “abcd…” nslinebreakbytruncatingmiddle // truncate middle of line: “ab…yz” */ paragraphstyle.basewritingdirection = nswritingdirectionrighttoleft;// 段落方向 /* nswritingdirectionnatural = -1, // determines direction using the unicode bidi algorithm rules p2 and p3 nswritingdirectionlefttoright = 0, // left to right writing direction nswritingdirectionrighttoleft = 1 */ }
以上就是关于macos开发-nsparagraphstyle的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product