标签: nsattributedstring

Swift - 将 html 字符串转换为属性字符串并再次转换回来时字体大小会增加

我有一个 textView,允许用户输入 NSAttributedString,转换为 html 字符串,然后存储到数据库中。我还有代码将 html 字符串转换回来并显示在 textView 中进行编辑。我的转换代码是

extension String {
    func htmlAttributedString() -> NSAttributedString? {
        guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }
        guard let html = try? NSMutableAttributedString(
            data: data,
            options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
            documentAttributes: nil) else { return nil }
        return html
    }
}

extension NSAttributedString {
    func htmlString() -> String? {
        let documentAttributes = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
        do {
            let htmlData = try self.data(from: NSMakeRange(0, self.length), documentAttributes:documentAttributes)
            if let htmlString = String(data:htmlData, …
Run Code Online (Sandbox Code Playgroud)

uitextview nsattributedstring ios swift

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

iOS/Swift:将 UITapGestureRecognizer 添加到 NSMutableAttributedString

我想知道是否可以将 a 添加UITapGestureRecognizerNSMutableAttributedString.

我想要做的是在字符限制的最后一个单词之后有一个“更多...”对象UITextView。为了使对象在最后一个单词之后立即出现,似乎最好使UITextView接受属性文本(而不是纯文本),然后附加类型为 的蓝色“更多...” NSMutableAttributedString

我见过人们通过将点击手势添加到封闭的来实现此目的的示例UITextView,但这还不够,因为我希望只有当他们点击属性字符串末尾的“更多...”时才会发生操作文本视图。我想看看是否有一种方法可以将点击手势直接添加到属性字符串中。

nsattributedstring ios uitapgesturerecognizer swift

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

带换行符 (\n) 的 AttributedString 无法正常工作

Continue reading我在最后添加。如果文本中有任何换行符,则无法继续阅读。这是 iOS 特有的错误还是我遗漏了什么?

let activityData.feed = "Hi this is \n \n stack overflow"

let formattedString = NSMutableAttributedString()
                formattedString.normal(activityData.feed!).bold(LanguageManager.shared.getLocale(key: "Continue Reading"))
                labelFeed.attributedText = formattedString
Run Code Online (Sandbox Code Playgroud)

属性字符串的扩展

extension NSMutableAttributedString {
    @discardableResult func bold(_ text:String) -> NSMutableAttributedString {
        let attrs:[String:AnyObject] = [NSFontAttributeName : UIFont.systemFont(ofSize: 16.0), NSForegroundColorAttributeName : UIColor.CNS_BlueColor]
        let boldString = NSMutableAttributedString(string:"\(text)", attributes:attrs)
        self.append(boldString)
        return self
    }

    @discardableResult func normal(_ text:String)->NSMutableAttributedString {
        let normal =  NSAttributedString(string: text)
        self.append(normal)
        return self
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

nsattributedstring ios nsmutableattributedstring swift3

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

检查 NSAttributed 字符串的前景色

我有一套NSAttributedStringUITextView我需要检查它的NSAttributedStringKey.foregroundColor价值。

char = attributedText.attributedSubstring(from: attributedRange)
Run Code Online (Sandbox Code Playgroud)

我尝试使用这个过滤器,但我真的不知道如何使用它。

let attr = char.attributes(at: 0, effectiveRange: nil)
attr.filter({ (<#(key: NSAttributedStringKey, value: Any)#>) -> Bool in
            //if true...
        })
Run Code Online (Sandbox Code Playgroud)

uitextview nsattributedstring ios swift

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

更改链接颜色和转换为 AttributedString 的 html 下划线颜色

我想为链接着色,并为从 html 接收的文本中的链接下划线赋予特殊颜色。

这就是我现在所拥有的:

...

public func htmlStyleAttributeText(text: String) -> NSMutableAttributedString? {

        if let htmlData = text.data(using: .utf8) {

            let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue]

            let attributedString = try? NSMutableAttributedString(data: htmlData, options: options, documentAttributes: nil)

            let attributes: [NSAttributedString.Key: AnyObject] = [NSAttributedString.Key.foregroundColor: UIColor.red]
            attributedString?.addAttributes(attributes, range: NSRange.init(location: 0, length: attributedString?.length ?? 0))
            return attributedString
        }

        return nil
    }
....
Run Code Online (Sandbox Code Playgroud)

这就是我得到的: 在此输入图像描述

我正在寻找的是文本的常规颜色、链接的红色和链接下划线的绿色

html nsattributedstring ios swift

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

Cocoa 自定义 NSView 的工具提示 Swift

我正在尝试创建带有粗体文本的工具提示。macOS 上的一些苹果应用程序使用此行为。我该如何实现这一目标?我目前的代码

btn.tooltip = "Open Options"
//tooltip doesn't accept attributed strings.
Run Code Online (Sandbox Code Playgroud)

这是我想要实现的目标的示例(使用此行为的 Xcode 的屏幕截图)。 在此输入图像描述

cocoa tooltip nsattributedstring swift

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

当调用特定子类中可用的方法时,超类类别中的performSelector失败

我有UIView类,它定义了操纵attributedTextUILabel和UITextView属性的方法.

@implementation UIView (replaceAttrText)
-(void) replaceAttrText: (NSString *)str {
    if ([self respondsToSelector: @selector(setAttributedText)]) {
        NSMutableAttributedString *labelText = [self template];

        // change it

        [self performSelector:@selector(setAttributedText) withObject: labelText];
    }
}
@end
Run Code Online (Sandbox Code Playgroud)

对于UILabel和UITextView,respondsToSelector都返回false(尽管它们响应setAttributedText),如果直接执行setAttributedText而不respondsToSelector检查则引发异常.

当类别直接在UILabel上实现(没有选择器)时,一切正常,但遗憾的是UILabel和UITextView没有具有attributesText属性的共同祖先.

我究竟做错了什么?谢谢!

objective-c uitextview nsattributedstring ios objective-c-category

0
推荐指数
1
解决办法
1075
查看次数

具有动态范围的NSAttributedString

我必须格式化一个包含3种样式的字符串.字符串类似于:

1.0000 of 2.000
Run Code Online (Sandbox Code Playgroud)

1.0000前景为红色,of字体较小,2.000必须为绿色.

问题是数字可以在任何范围内,因此第一个和第二个数字可以由4,5,6个字符组成.

如何执行这样的字符串格式化?

编辑-----------------我添加了一些信息:字符串保持其格式.例如,这可能是它的模板:N of N

objective-c nsattributedstring

0
推荐指数
1
解决办法
1243
查看次数

我可以在swift中制作一个文本有多种颜色的按钮吗?

我可以在swift中制作一个文本有多种颜色的按钮吗?按钮文本将动态更改,因此我无法生成图像.

我试图用不同的颜色制作两个属性字符串,连接它们,并将按钮的文本设置为.不幸的是,这并没有保留不同的颜色,只是添加了描述字符串末尾的nsattribtues的明文.

let currDescString = NSAttributedString(string: descriptor)
let editString = NSAttributedString(string: "(edit)", attributes: [NSForegroundColorAttributeName : UIColor.blueColor()])
let editDescriptionString = NSAttributedString(string: "\(descriptor) \(editString)")
subBttn.setAttributedTitle(editDescriptionString, forState: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)

我希望currDescString为黑色,editString为蓝色...在第3行,我尝试连接这些,在第4行,我尝试设置标题.如上所述的同样问题仍然存在.

uibutton nsattributedstring ios swift

0
推荐指数
1
解决办法
1812
查看次数

在现有NSMutableAttributed字符串中添加属性

我正在研究一些属性字符串格式.我需要在现有的NSMutableAttributed字符串中添加删除线.在下面给出的例子中是否可能.

First Task For Bhasin
30 Jun 2017 12:05 PM till 30 Jun 2017 01:05 PM{
NSBackgroundColor = "<CGColor 0x6100000934c0> [<CGColorSpace 0x6080000303c0> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 0 0 )";
NSColor = "<CGColor 0x6180002a2ac0> [<CGColorSpace 0x610000230920> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 1 0 0 1 )";
NSFont = "<UICTFont: 0x7fac5476a0b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 15.00pt";
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent …
Run Code Online (Sandbox Code Playgroud)

objective-c nsattributedstring strikethrough ios

0
推荐指数
1
解决办法
1607
查看次数