回答
贾巴尔是正确的。创建一个UIButton对象,将其类型设置为“Custom”,然后可以给它一个标题。将动作连接到推送或呈现您的注册视图控制器的方法,您将全部设置。
你可能想给一些指示该文本是一个可点击的链接的用户。将文本的颜色设置为蓝色。另一个完全独立的选项是使用NSAttributedString
(具有下划线属性)向用户指示文本是可点击的。这需要an Open Source replacement for UITextView(您可以从this related question了解更多信息)。
嘿所有,卡里姆,迈克尔,非常感谢....但我有另一个查询,我找不到任何方法来指示用户文本是可点击的,即有文本的蓝色。另一件事,我可以有“手”符号,当用户移动光标在该字符串.. ??? – Krunal 2012-01-04 09:55:03
我想你是要求iPhone应用程序和iPhone应用程序不会显示任何光标在屏幕上,对不对? – DivineDesert 2012-01-04 12:28:00
Oopss ...是的,我知道了... – Krunal 2012-01-04 12:44:42
首先,构建属性串并保存交互式文本和文本本身的范围。
然后,绘制属性串与CoreText框架,保持CTFrameRef。
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, bounds);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CFRelease(framesetter);
CTFrameDraw(frame, context);
//CFRelease(frame);
CGPathRelease(path);
最后,重载[查看touchesEnded:(NSSet中*)触及withEvent:方法(的UIEvent *)事件]是这样的:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *allTouches = [event allTouches];
if ([allTouches count] == 1) {
UITouch *touch = (UITouch *)[allTouches anyObject];
CGPoint touchPoint = [touch locationInView:self];
// Convert to coordinate system of current view
touchPoint.y -= self.bounds.size.height;
touchPoint.y *= -1;
CGPathRef statusPath = CTFrameGetPath(statusCTFrame);
NSString *touchedInterStr = nil;
if (CGPathContainsPoint(statusPath, NULL, touchPoint, FALSE)) {
touchedInterStr = [self getInteractiveStringInFrame:statusCTFrame atPosition:touchPoint];
} else {
return ;
}
}
}
- (NSString *)getInteractiveStringInFrame:(CTFrameRef)frame atPosition:(CGPoint)point
{
CGPathRef path = CTFrameGetPath(frame);
CGRect rect;
CGPathIsRect(path, &rect);
// Convert point into rect of current frame, to accurately perform hit testing on CTLine
CGPoint pointInsideRect = point;
pointInsideRect.x = point.x - rect.origin.x;
CFArrayRef lines = CTFrameGetLines(statusCTFrame);
CFIndex count = CFArrayGetCount(lines);
CGFloat curUpBound = rect.origin.y + rect.size.height;
CFIndex touchedIndex = -1;
for (CFIndex pos = 0; pos < count; pos++) {
CTLineRef curLine = CFArrayGetValueAtIndex(lines, pos);
CGFloat ascent, descent, leading;
CTLineGetTypographicBounds(curLine, &ascent, &descent, &leading);
CGFloat curHeight = ascent + descent + leading;
if (pointInsideRect.y >= curUpBound-curHeight && pointInsideRect.y <= curUpBound){
touchedIndex = CTLineGetStringIndexForPosition(curLine, pointInsideRect); // Hit testing
break;
}
curUpBound -= curHeight;
}
if (touchedIndex == -1)
return nil;
NSEnumerator *enumerator = [interactiveStrs objectEnumerator];
InteractiveString *curString;
while ((curString = (InteractiveString *)[enumerator nextObject])) {
if (NSLocationInRange(touchedIndex, curString.range)) {
return curString.content;
}
}
return nil;
}
你得到touchesEnded交互式文本,那么你可以做任何你想做的事情,比如触发委托方法。
PS:这是旧时尚的解决方案,我听说的iOS5已经像来实现需求的增强的UIWebView提供的东西,也许你可以检查苹果的SDK文档库。
什么是“InteractiveString”我找不到它.... – 2012-05-31 06:24:45
我认为下面你会得到你想要的...
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *allTouches = [event allTouches];
if ([allTouches count] == 1)
{
UITouch *touch = (UITouch *)[allTouches anyObject];
CGPoint touchPoint = [touch locationInView:self];
// Convert to coordinate system of current view
touchPoint.y -= self.bounds.size.height;
touchPoint.y *= -1;
CGPathRef statusPath = CTFrameGetPath(statusCTFrame);
NSString *touchedInterStr = nil;
if (CGPathContainsPoint(statusPath, NULL, touchPoint, FALSE))
{
touchedInterStr = [self getInteractiveStringInFrame:statusCTFrameatPosition:touchPoint];
}
else
{
return ;
}
}
}
有一个中间地带是值得一提。使用UIButton
与UIButtonTypeCustom
有一些缺点,即您对文本样式的控制有限(例如,您无法使其右对齐或左对齐,它总是居中)。
使用NSAttributedString
可以矫枉过正,而且很难实现,如果你不熟悉NSAttributedString
,CoreText等
我使用的解决方案是UIControl
一个简单的子类,允许更多的定制比继承UIButton
少麻烦比NSAttributedString
等。 UIControl
方法只适用于整个字符串是“链接”的情况。
你会做这样的事情在你的UIControl
子类:
- 添加
UILabel
子视图在init方法 - 配置
UILabel
与所需backgroundColor
,textColor
,textAlignment
等,包括更改UILabel
如果您希望背景显示为其iOS应用中Twitter的推文链接 - 覆盖
setHighlighted:
方法以自定义外观ð觉得触摸更改/跟踪 - 添加
@property
制定者在.h
文件,以便所属的类可以设置状态
以一个按钮,并要设置为超链接文本按钮设置标题。您可以在iOS 7和iOS 8中选择默认和自定义按钮类型按钮,但对于iOS 7以下的版本,您只需要按钮类型自定义。您可以更改文本的颜色以突出显示用户界面的文本,并添加您希望添加到该按钮上的任何操作。它会像魅力一样运作,祝你好运。
看一看TTTAttributedLabel。配置链接,手机等非常简单。
- 1. 在NativeScript中的“文本链接”上设置标签
- 2. 为#标签解析文本并使用JavaScript代替链接
- 3. 使用jQuery为超链接中的文本添加span标签
- 4. 锚标签直接链接需要为忽略
- 5. 从链接标签
- 6. 设置标签文本
- 7. 将文本框转换为标签
- 8. 将NSString写为文本标签
- 9. 将文本更改为超链接
- 10. Javascript将文本转换为链接
- 11. 将span标签添加到ruby链接?
- 12. 将标签文本设置为gridview总行数
- 13. 将第二行文本设置为第一个标签
- 14. 如何将标签文本设置为JTextField输入
- 15. tkinter columnconfigure将我的标签文本设置为中心
- 16. 如何将UITextView中的文本设置为链接到URL
- 17. 将文本链接转换为<a>标记
- 18. JointJS:使链接标签可以在链接本身上移动
- 19. Haml的超链接标签标签内
- 20. AngularJS转换链接到链接标签
- 21. 在Rails URL中链接标签链接
- 22. 链接标签内的链接
- 23. 为外部链接添加标签
- 24. jQuery的删除与此链接后一定标签和整个文本链接
- 25. 如何为DataRepeater链接标签设置唯一ID
- 26. 如何将数组项目作为链接标签中的单独链接
- 27. Wicket链接 - 是否添加了设置文本所需的标签?
- 28. 将Twitter哈希标签变成链接并链接到可点击的链接
- 29. 获取链接上的标签元素文本点击
- 30. 用链接点击创建文本框(标签)Javascript
不能得到你的意思。你的意思是如何在文本上制作一个超链接,然后点击时,转到注册视图? – Raptor 2012-01-04 08:59:57
最好使用自定义类型的UIbutton而不是lablel。 – 2012-01-04 09:05:38
听起来像这样。 http://stackoverflow.com/questions/4743457/how-to-customize-uilabel-clickable – Ilanchezhian 2012-01-04 09:21:58