相关疑难解决方法(0)

在UILabel的NSAttributedString中创建可点击的"链接"?

我一直在搜索这几个小时,但我失败了.我可能甚至不知道我应该寻找什么.

许多应用程序都有文本,在本文中是圆角矩形的Web超链接.当我点击它们UIWebView打开.让我感到困惑的是,他们经常有自定义链接,例如,如果单词以#开头,它也是可点击的,应用程序通过打开另一个视图来响应.我怎样才能做到这一点?是否有可能UILabel或我需要UITextView或其他什么?

hyperlink nsattributedstring uilabel ios uitapgesturerecognizer

220
推荐指数
14
解决办法
19万
查看次数

在UIButton中加下划线

任何人都可以建议如何强调UIButton的标题?我有一个自定义类型的UIButton,我希望标题有下划线,但Interface Builder没有提供任何选项.

在Interface Builder中为按钮选择字体选项时,它提供了选择无,单,双,颜色的选项,但这些选项都不会对按钮上的标题进行任何更改.

任何帮助赞赏.

formatting text uibutton

135
推荐指数
9
解决办法
9万
查看次数

检测iOS中UITextView中属性文本的点击

我有一个显示NSAttributedString的UITextView.这个字符串包含我想要点击的单词,这样当它们被点击时我会被回叫以便我可以执行一个动作.我意识到UITextView可以检测URL上的点击并回调我的委托,但这些不是URL.

在我看来,iOS7和TextKit的强大功能现在应该可行,但我找不到任何示例,我不知道从哪里开始.

我知道现在可以在字符串中创建自定义属性(虽然我还没有这样做),也许这些对于检测是否有一个神奇的单词被点击是有用的?在任何情况下,我仍然不知道如何拦截该敲击并检测敲击发生在哪个单词上.

请注意,不需要iOS 6兼容性.

objective-c uitextview ios textkit

114
推荐指数
7
解决办法
6万
查看次数

如何在iOS中的UILabel上显示HTML字符串?

我正在获得HTML格式的标题

<p> <span style ="color:#000000;"> <strong>示例</ strong> </ span> </ p>

我需要在UILabel中显示这个HTML字符串.颜色代码和字体大小应与HTML中的相同.当我将HTML字符串转换为NSString时,只有文本"示例"即将到来,而不是颜色.

有什么解决方案吗?

Thnx提前

直到现在我尝试通过以下方式使用NSAttributedString,但通过这种方式整个HTML正在打印:

UIFont *font = [UIFont systemFontOfSize:14.0];
UIFont *secondFont = [UIFont systemFontOfSize:10.0];

NSMutableDictionary *firstAttributes;
NSMutableDictionary *secondAttributes;

NSDictionary *firstAttributeFont = @{NSFontAttributeName:font};
NSDictionary *secondAttributeFont = @{NSFontAttributeName:secondFont};

[firstAttributes addEntriesFromDictionary:firstAttributeFont];
[secondAttributes addEntriesFromDictionary:secondAttributeFont];

[firstAttributes addEntriesFromDictionary:@{NSForegroundColorAttributeName:[UIColor clearColor]}];
[secondAttributes addEntriesFromDictionary:@{NSForegroundColorAttributeName:[UIColor clearColor]}];



NSString* completeString = [NSString stringWithFormat:@"%@",strTitle];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]     initWithString:completeString];
[attributedString setAttributes:firstAttributes range:[completeString rangeOfString:strTitle]];
//   [attributedString setAttributes:secondAttributes range:[completeString rangeOfString:self.secondAttributeText]];
Cell.lbl_Title.attributedText = attributedString;
Run Code Online (Sandbox Code Playgroud)

ios

50
推荐指数
5
解决办法
6万
查看次数

UILabel触摸点的字符索引

对于a UILabel,我想知道从触摸事件接收的特定点处的哪个字符索引.我想使用Text Kit为iOS 7解决这个问题.

由于UILabel不提供对它的访问NSLayoutManager,我根据这样UILabel的配置创建了自己的:

- (void)textTapped:(UITapGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        CGPoint location = [recognizer locationInView:self];

        NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:self.attributedText];
        NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
        [textStorage addLayoutManager:layoutManager];
        NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.bounds.size];
        [layoutManager addTextContainer:textContainer];

        textContainer.maximumNumberOfLines = self.numberOfLines;
        textContainer.lineBreakMode = self.lineBreakMode;


        NSUInteger characterIndex = [layoutManager characterIndexForPoint:location
                                                          inTextContainer:textContainer
                                 fractionOfDistanceBetweenInsertionPoints:NULL];

        if (characterIndex < textStorage.length) {
            NSRange range = NSMakeRange(characterIndex, 1);
            NSString *value = [self.text substringWithRange:range];
            NSLog(@"%@, %zd, %zd", value, range.location, range.length); …
Run Code Online (Sandbox Code Playgroud)

objective-c ios7 textkit

24
推荐指数
4
解决办法
1万
查看次数

转动UILabel的某些部分就像UIButton一样?

我有一个属性字符串UILabel,我能够为文本的某些部分着色

let text = "Why did \(event) give \(event2) a 5 stars review? Details here. "
let linkTextWithColor = "Why did"
let range = (text as NSString).rangeOfString(linkTextWithColor)

let attributedString = NSMutableAttributedString(string:text)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor() , range: range)

labelEvent.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)

现在我想让文本的某些部分可以替换,比如UIButton,如何做到这一点?

例1

例1

例2

例2

我需要有蓝色文本来响应触摸,并运行一个特定的功能,如一个UIButton.非常感谢帮助,谢谢.

uibutton uilabel ios swift

19
推荐指数
3
解决办法
1万
查看次数

使用swift在UILabel中单击NSAttributedString事件

假设我有一个AttributedString:"已经有一个帐户?登录!".我将此字符串放在UILabel中.现在,当用户点击"登录!"时,当前的viewController应该转到另一个viewController,或者在点击登录时调用某个函数.

任何代码或建议都应该没问题.

nsattributedstring uilabel ios swift

8
推荐指数
1
解决办法
2万
查看次数

如何使警报控制器中的可点击链接迅速

我的应用截图

我想要这样的警报对话框。其中包含一个可点击的链接,例如蓝色突出显示的文本。当用户单击该文本时,它将在浏览器中打开一个链接。为此,我正在使用第三方库SimpleAlert

这是我的代码...

func popUpMsg(){

    let msg = "\n Hi Rahul! Welcome back to the early access queue for the app, Once you've been accepted into the beta, we'll notify you using the contact info you have on file with Facebook. \n\n But just a second, if you haven't already done so, you can increase your odds of jumping ahead in the queue by telling us a little about yourself. Please complete our 4-question survey to give it a shot. …
Run Code Online (Sandbox Code Playgroud)

ios swift uialertcontroller

6
推荐指数
0
解决办法
4299
查看次数

iOS - Swift 如何在 UILabel 中单击链接

这是我的代码,显示 UILabel 中的链接

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textFiled: UITextField!

    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let attrStr = try! NSAttributedString(
            data: "This is a link <a href='http://www.google.com'>google</a>".dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
            options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
            documentAttributes: nil)
        label.attributedText = attrStr
    }

}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

从图片中可以看到,它正确显示了我想要的链接,但该链接无法单击跳转到网页视图或其他视图。实际上,我需要自定义链接点击事件,但我真的不知道:

  1. 如何获取链接的 URL。
  2. 如果有多个链接,我需要做什么。

感谢您首先检查这个问题。

html tags uilabel swift

6
推荐指数
1
解决办法
2万
查看次数

如何在swift 4中检测UILabel中的链接?

我使用ActiveLabel作为第三方库,在特定单词的标签中建立链接.代码适用于Swift 3和3.2.但不适用于swift 4.

我用的代码如下

let customType1 = ActiveType.custom(pattern: "\\sTerms & Conditions\\b") //Looks for "are"
            labelTc.enabledTypes.append(customType1)
            labelTc.customize { (label) in
                labelTc.text = "UserAgreement".localized
                label.numberOfLines = 0
                label.lineSpacing = 4
                label.textColor = UIColor(red: 131 / 255, green: 147 / 255, blue: 168 / 255, alpha: 1)
                //Custom types
                label.customColor[customType1] = Constant.AppColor.greenMeadow
                label.customSelectedColor[customType1] = Constant.AppColor.greenMeadow
                label.configureLinkAttribute = { (type, attributes, isSelected) in
                    var atts = attributes
                    switch type {
                    case customType1:
                        atts[NSAttributedStringKey.font._rawValue as String] = UIFont(name: self.labelTc.font.fontName, size: 15.0) …
Run Code Online (Sandbox Code Playgroud)

uilabel ios swift

6
推荐指数
1
解决办法
3342
查看次数