是否可以在 UILabel/UITextView 中添加 UIButton? [英] Is it possible to add a UIButton within a UILabel / UITextView?

查看:102
本文介绍了是否可以在 UILabel/UITextView 中添加 UIButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个有趣的场景.我从服务器发送了以下文本:

Here is an interesting scenario. I have the following text sending from server:

我通过#option#上学

I go to school by #option#

在iOS App中,我必须将其转换为:

In iOS App, I have to convert it into:

我通过[UIButton]上学

I go to school by [UIButton]

挑战是测量前面的文本(I go to school by")的长度,以设置UIButtonorigin)>.当UILabel有多行,UIButton有多个出现时,问题会比较严重,比如:

The challenge is to measure the length of the previous text ("I go to school by "), in order to set the location (the origin) of the UIButton. The problem will be more problematic when the UILabel has multiple lines, and multiple appearance of UIButton, such as:

我通过#option#上学.Lorem ipsum dolor 坐 amet,consectetur adipiscing 精英.Phasellus fringilla、massa ut pharetra ultricies、elit nunc accumsan libero、ut elementum est justo sed lacus.Proin mitellus, suscipit in magna a, auctor laoreet eros.#option# Quisque rhoncus ac mauris ac interdum.Etiam a odio augue.Mauris porttitor purus ac maximus faucibus.Suspendisse velit felis、varius lobortis turpis venenatis、tempor placerat magna.semper justo 中的整数,ut pretium nunc.Aliquam laoreet vehicula finibus.#option#

I go to school by #option#. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus fringilla, massa ut pharetra ultricies, elit nunc accumsan libero, ut elementum est justo sed lacus. Proin mi tellus, suscipit in magna a, auctor laoreet eros. #option# Quisque rhoncus ac mauris ac interdum. Etiam a odio augue. Mauris porttitor purus ac maximus faucibus. Suspendisse velit felis, varius lobortis turpis venenatis, tempor placerat magna. Integer in semper justo, ut pretium nunc. Aliquam laoreet vehicula finibus.#option#

以下是我能想到的一些解决方案:

Here are some of my solutions I can think of :

方法一

NSString * testText = @"demo title";
CGSize stringSize = [testText sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];

此方法可以测量单行文本的大小,但对于多行文本,CGSize会不正确.

This method can measure the size of a single line text, but for multiple lines text, the CGSize will be incorrect.

方法二

NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:@"hello"];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer *textContainer = [[NSTextContainer alloc] init];

[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];

//Figure out the bounding rectangle
NSRect stringRect = [layoutManager boundingRectForGlyphRange:NSMakeRange(0, [layoutManager numberOfGlyphs]) inTextContainer:textContainer];

这要复杂得多.(还在摸索如何构建布局)

This is much more complex. (Still figuring out how to build the layout)

我的问题是:有没有更简单的方法来实现我的目标?

My question is: Any simpler method to achieve my goal?

推荐答案

@vienvu 删除的答案最接近通过使用 YYText 库.

@vienvu's deleted answer is closest to solve the problem by making use of YYText library.

NSString *text = @"Test adding UIButton #option# inline";
NSMutableAttributedString *method2text = [[NSMutableAttributedString alloc] initWithString:text];
UIFont *font = [UIFont systemFontOfSize:20];
NSMutableAttributedString *attachment = nil;

NSRange range = [[method2text string] rangeOfString:@"#option#"];
if(range.location != NSNotFound) {
    UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    tf.borderStyle = UITextBorderStyleRoundedRect;
    attachment = [NSMutableAttributedString yy_attachmentStringWithContent:tf contentMode:UIViewContentModeBottom attachmentSize:tf.frame.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
    [method2text replaceCharactersInRange:range withString:@""];
    [method2text insertAttributedString:attachment atIndex:range.location];

}

theLabel = [YYLabel new];
theLabel.userInteractionEnabled = YES;
theLabel.numberOfLines = 0;
theLabel.frame = CGRectMake(0, 100, 320, 320);
theLabel.center = self.view.center;
theLabel.attributedText = method2text;
[self.view addSubview:theLabel];

其中 theLabelYYLabel.我发现 YYTextView 无法选择 UI 元素.类似的代码也适用于 UIButton.

where theLabel is a YYLabel. I found that YYTextView cannot select the UI elements. Similar codes are workable for UIButton too.

这篇关于是否可以在 UILabel/UITextView 中添加 UIButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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