我想在UILabel上做一个轻击动作。 例如:如果我们有“My name is XYZ”这样的字符串,在这种情况下我只想为“XYZ”添加点击操作。
如何在iOS(swift)
中执行此操作答案 0 :(得分:0)
有控制调用TTTAttributedLabel可以在Objective-C中处理相同的内容所以你可以使用它,
TTTAttributedLabel *tttLabel = <# create the label here #>;
NSString *labelText = @"Lost? Learn more.";
tttLabel.text = labelText;
NSRange r = [labelText rangeOfString:@"Learn more"];
[tttLabel addLinkToURL:[NSURL URLWithString:@"action://show-help"] withRange:r];
然后,在你的TTTAttributedLabelDelegate:
中- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
if ([[url scheme] hasPrefix:@"action"]) {
if ([[url host] hasPrefix:@"show-help"]) {
/* load help screen */
} else if ([[url host] hasPrefix:@"show-settings"]) {
/* load settings screen */
}
} else {
/* deal with http links here */
}}
对于Swift,你可以做一些像
这样的事情let name = "tomo"
let string = "My name is \(name)"
label.text = string
let nsString = string as NSString
let range = nsString.rangeOfString(name)
let url = NSURL(string: "action://users/\(name)")!
label.addLinkToURL(url, withRange: range)
答案 1 :(得分:-1)
如何在UIButton旁边的UIView中设置标签?
UIView *view = [UIView new];
UILabel *label = [UILabel new];
UIButton *button = [UIButton new];
添加这些约束以查看:
|[label][button]|
label.text = @"My name is"
[button setTitle:yourName forState:UIControlStateNormal];
通过将标签和按钮的背景颜色设置为clearColor,并将视图的背景设置为whiteColor(例如),您应该看不到那么大的差异。