Heim > Web-Frontend > js-Tutorial > Hauptteil

Über MacOS-Entwicklung – NSParagraphStyle

一个新手
Freigeben: 2017-09-27 10:12:18
Original
1493 Leute haben es durchsucht
  • Einführung

  • Verwendung

  • Absatzhöhe berechnen

  • Verwandte Attribute

    • alignment - 对齐方式
      firstLineHeadIndent - 首行缩进
      headIndent -  缩进
      tailIndent -  尾部缩进
      lineBreakMode - 断行方式
      maximumLineHeight - 最大行高
      minimumLineHeight - 最低行高
      lineSpacing - 行距
      paragraphSpacing  -段距
      paragraphSpacingBefore - 段首空间
      baseWritingDirection - 句子方向
      lineHeightMultiple - 可变行高乘因数
      hyphenationFactor - 连字符属性
      Nach dem Login kopieren

Einführung

NSParagraphStyleAttributeName wird verwendet, um NSAttributedString festzulegen, ähnlich wie NSFontAttributeName.
Der Unterschied besteht darin: NSParagraphStyleAttributeName dient zum Festlegen des Absatzstils, NSFontAttributeName dient zum Festlegen der Schriftart.


Verwendungsmethode

- (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];
}
Nach dem Login kopieren

Absatzhöhe berechnen

 - (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;

}
Nach dem Login kopieren

Verwandte Attribute

  • NSMutableParagraphStyle und NSParagraphStyle umfassen die folgenden Attribute

alignment - 对齐方式
firstLineHeadIndent - 首行缩进
headIndent -  缩进
tailIndent -  尾部缩进
lineBreakMode - 断行方式
maximumLineHeight - 最大行高
minimumLineHeight - 最低行高
lineSpacing - 行距
paragraphSpacing  -段距
paragraphSpacingBefore - 段首空间
baseWritingDirection - 句子方向
lineHeightMultiple - 可变行高,乘因数。
hyphenationFactor - 连字符属性
Nach dem Login kopieren
(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
*/
}
Nach dem Login kopieren

Das obige ist der detaillierte Inhalt vonÜber MacOS-Entwicklung – NSParagraphStyle. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!