【问题标题】:How to create a bold and normal font text in single UILabel in iPhone?如何在 iPhone 的单个 UILabel 中创建粗体和普通字体文本?
【发布时间】:2012-06-01 06:26:00
【问题描述】:

我想在 UILabel 中创建如下示例文本..

Muthu:嗨,我正在去办公室的路上..
等几分钟……

这种类型的文字有UITableviewCustom单元格有UILabel。

如何在UILabel中创建?

【问题讨论】:

    标签: iphone ios ios4 ios5


    【解决方案1】:

    使用以下代码并包含来自以下链接“https://github.com/AliSoftware/OHAttributedLabel”的 .h 和 .m 文件。

    -(void)addTitleView{
    
        NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:@"Muthu: Hi I am on the way to office..wait for the few mins..."];
        [attrStr setFont:[UIFont fontWithName:@"Verdana" size:16]];
        [attrStr setTextColor:[UIColor whiteColor]];
        [attrStr setFont:[UIFont fontWithName:@"Verdana-Bold" size:16] range:NSMakeRange(0,6)];
        
        OHAttributedLabel *titleLabel = [[OHAttributedLabel alloc] init];
        titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [titleLabel setFrame:CGRectMake(0, 0, 200, 35)];
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        titleLabel.adjustsFontSizeToFitWidth = YES;
        titleLabel.attributedText = attrStr;
        titleLabel.textAlignment = UITextAlignmentJustify;
        [self.view addSubview:titleLabel];
    }
    

    【讨论】:

      【解决方案2】:

      使用支持属性字符串的 UILabel 是 TTTAtributedLabelOHAttributedLabel

      我使用 OHAttributedLabel。示例参考this ansewr,使用 setTextBold 表示粗体。

      【讨论】:

        【解决方案3】:

        你必须使用NSAttributedString。有许多开源组件可以为您做到这一点。看看this

        【讨论】:

          猜你喜欢
          • 2012-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多