如何在其中创建带有粗体和普通文本的UILabel或UITextView? [英] How to create a UILabel or UITextView with bold and normal text in it?

查看:96
本文介绍了如何在其中创建带有粗体和普通文本的UILabel或UITextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用粗体和普通文本创建 UILabel UITextView

I am trying to create a UILabel or UITextView with bold and normal text inside.

我已经浏览过attributesstring,但是当我在自定义单元格的标签中设置它时,它不显示任何文本。

I have gone through the attributedstring but when I am setting this in label of my custom cell it doesn't display any text.

我还使用了 UITextView setContentToHTMLString:方法,但它没有记录,应用程序被拒绝。

I have also used the UITextView setContentToHTMLString: method, but it is undocumented and app get rejected.

任何人都可以给出某种解决方案吗?

Can anyone give some sort of solution to this?

推荐答案

使用 NSAttributedString 在单个标签中设置多个字体文本&使用 CATextLayer 进行渲染:

Use "NSAttributedString" to set multiple font text in a single label & use CATextLayer to render it:

只需 #importNSAttributedString + Attributes.h

然后像这样实现:

NSString *string1 = @"Hi";

NSString *string2 = @"How are you ?";

NSMutableAttributedString *attr1 = [NSMutableAttributedString attributedStringWithString:string1];

[attr1 setFont:[UIFont systemFontOfSize:20]];

NSMutableAttributedString *attr2 = [NSMutableAttributedString attributedStringWithString:string2]

[attr2 setFont:[UIFont boldSystemFontOfSize:20]];

[attr1 appendAttributedString:attr2]

CATextLayer *textLayer = [CATextLayer layer];

layer.string = attr1;

layer.contentsScale = [[UIScreen mainScreen] scale];

(Your_text_label).layer = textLayer;

OR (if you want to render on a view directly)

[(Your_View_Name).layer addSublayer:textLayer];

这篇关于如何在其中创建带有粗体和普通文本的UILabel或UITextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆