【问题标题】:how to set different font style on different UIlabel text line in ios using core text如何使用核心文本在 ios 中的不同 UIlabel 文本行上设置不同的字体样式
【发布时间】:2016-11-18 05:23:30
【问题描述】:

我想让标签格式线这个动态形状的标签文本字在不同大小上如何使用核心文本框架

请在Dynamic arrange label textDiffent font style arrange上生成动态形状时显示此类型格式

请建议使用自定义库来制作这种类型的格式

【问题讨论】:

  • 使用 NSAttributedString 和 label.attributedText 而不是 label.text
  • 我使用了 NSAttributed 字符串,但没有像这样做出正确的格式..所以建议其他选项..或方法

标签: ios objective-c swift uilabel core-text


【解决方案1】:

我只是用 NSAttributedString 告诉你方向,你必须弄清楚其余的细节,比如正确的字体、大小、样式、行间距等。(提示:检查 NSMutableParagraphStyle)

    let attr = NSMutableAttributedString(string: "")
    attr.appendAttributedString(NSAttributedString(string: "The\n", attributes: [NSFontAttributeName: UIFont.boldSystemFontOfSize(60), NSForegroundColorAttributeName: UIColor.greenColor()]))
    attr.appendAttributedString(NSAttributedString(string: "HAPPINESS OF\n", attributes: [NSFontAttributeName: UIFont.systemFontOfSize(45), NSForegroundColorAttributeName: UIColor.redColor()]))
    attr.appendAttributedString(NSAttributedString(string: "YOUR LIFE\n", attributes: [NSFontAttributeName: UIFont.italicSystemFontOfSize(40), NSForegroundColorAttributeName: UIColor.purpleColor()]))
    attr.appendAttributedString(NSAttributedString(string: "DEPENDS UPON", attributes: [NSFontAttributeName: UIFont.boldSystemFontOfSize(15), NSForegroundColorAttributeName: UIColor.brownColor()]))
    label.attributedText = attr

【讨论】:

  • @RGE 是的,比使成为可能,但我想使“你的生活”中的字符空间与行长上一行“幸福的”动态相等,以使两条线等长..
【解决方案2】:

使用objective-C,您需要制作4个具有不同字体名称和大小的NSMutableAttributedString,然后使用“appendAttributedString”附加它们以获得所需的结果。使用这种方法,您只需为所有字符串制作一帧。

请参阅下面的代码以供参考。

//frame that contains all text
textFrame = [[UILabel alloc] init];
[textFrame setTextAlignment:NSTextAlignmentCenter];
[textFrame setNumberOfLines:0];
[self.view addSubview:textFrame];

NSString *text1 = @"THE";
NSString *text2 = @"\nHAPPINESS OF";
NSString *text3 = @"\nYOUR LIFE";
NSString *text4 = @"\nDEPENDS UPON";

//change font name and size according to your need.
UIFont *text1Font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:10];
NSMutableAttributedString *attributedString1 =
[[NSMutableAttributedString alloc] initWithString:text1 attributes:@{
NSFontAttributeName : text1Font}];
NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle1 setAlignment:NSTextAlignmentCenter];
[paragraphStyle1 setLineSpacing:4];
[attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [attributedString1 length])];

UIFont *text2Font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:16];
NSMutableAttributedString *attributedString2 =
[[NSMutableAttributedString alloc] initWithString:text2 attributes:@{NSFontAttributeName : text2Font }];
NSMutableParagraphStyle *paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle2 setLineSpacing:4];
[paragraphStyle2 setAlignment:NSTextAlignmentCenter];
[attributedString2 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle2 range:NSMakeRange(0, [attributedString2 length])];

UIFont *text3Font = [UIFont fontWithName:@"HelveticaNeue" size:14];
NSMutableAttributedString *attributedString3 =
[[NSMutableAttributedString alloc] initWithString:text3 attributes:@{NSFontAttributeName : text3Font }];
NSMutableParagraphStyle *paragraphStyle3 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle3 setAlignment:NSTextAlignmentCenter];
[attributedString3 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle3 range:NSMakeRange(0, [attributedString3 length])];

UIFont *text4Font = [UIFont fontWithName:@"HelveticaNeue" size:14];
NSMutableAttributedString *attributedString4 =
[[NSMutableAttributedString alloc] initWithString:text4 attributes:@{NSFontAttributeName : text4Font }];
NSMutableParagraphStyle *paragraphStyle4 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle4 setAlignment:NSTextAlignmentCenter];
[attributedString4 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle4 range:NSMakeRange(0, [attributedString4 length])];

[attributedString1 appendAttributedString:attributedString2];
[attributedString1 appendAttributedString:attributedString3];
[attributedString1 appendAttributedString:attributedString4];

[textFrame setAttributedText:attributedString1];
[textFrame sizeToFit];
//change frame size as per your need.
[textFrame setFrame:CGRectMake(10, 0, 136, 97)];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2011-11-14
    • 1970-01-01
    相关资源
    最近更新 更多