ios 如何连接NSAttributedString?

am46iovg  于 2023-02-20  发布在  iOS
关注(0)|答案(9)|浏览(114)

在合并字符串之前,我需要搜索一些字符串并设置一些属性,所以让NSStrings -〉Concatenate them -〉Make NSAttributedString不是一个选项,有没有办法将attributedString连接到另一个attributedString?

roqulrg3

roqulrg31#

我建议您使用@Linuxios建议的单个可变属性字符串,下面是另一个示例:

NSMutableAttributedString *mutableAttString = [[NSMutableAttributedString alloc] init];

NSString *plainString = // ...
NSDictionary *attributes = // ... a dictionary with your attributes.
NSAttributedString *newAttString = [[NSAttributedString alloc] initWithString:plainString attributes:attributes];

[mutableAttString appendAttributedString:newAttString];

但是,为了获得所有选项,您也可以创建一个可变属性字符串,由包含已放在一起的输入字符串的格式化NSString组成,然后使用addAttributes: range:将属性添加到包含输入字符串的范围中,但我推荐前一种方法。

gfttwv5a

gfttwv5a2#

如果使用Swift,只需重载+运算符,就可以像连接普通字符串一样连接它们:

// concatenate attributed strings
func + (left: NSAttributedString, right: NSAttributedString) -> NSAttributedString
{
    let result = NSMutableAttributedString()
    result.append(left)
    result.append(right)
    return result
}

现在,您只需添加它们即可将它们连接起来:

let helloworld = NSAttributedString(string: "Hello ") + NSAttributedString(string: "World")
qfe3c7zg

qfe3c7zg3#

Swift 3:简单地创建一个NSMutableAttributedString,并将属性化字符串附加到其中。

let mutableAttributedString = NSMutableAttributedString()

let boldAttribute = [
    NSFontAttributeName: UIFont(name: "GothamPro-Medium", size: 13)!,
    NSForegroundColorAttributeName: Constants.defaultBlackColor
]

let regularAttribute = [
    NSFontAttributeName: UIFont(name: "Gotham Pro", size: 13)!,
    NSForegroundColorAttributeName: Constants.defaultBlackColor
]

let boldAttributedString = NSAttributedString(string: "Warning: ", attributes: boldAttribute)
let regularAttributedString = NSAttributedString(string: "All tasks within this project will be deleted.  If you're sure you want to delete all tasks and this project, type DELETE to confirm.", attributes: regularAttribute)
mutableAttributedString.append(boldAttributedString)
mutableAttributedString.append(regularAttributedString)

descriptionTextView.attributedText = mutableAttributedString

swift5更新:

let captionAttribute = [
        NSAttributedString.Key.font: Font.captionsRegular,
        NSAttributedString.Key.foregroundColor: UIColor.appGray
    ]
3pmvbmvn

3pmvbmvn4#

试试这个:

NSMutableAttributedString* result = [astring1 mutableCopy];
[result appendAttributedString:astring2];

其中astring1astring2NSAttributedString s。

mbskvtky

mbskvtky5#

    • 2020年|SWIFT 5.1:**

您可以通过以下方式添加2 NSMutableAttributedString

let concatenated = NSAttrStr1.append(NSAttrStr2)

另一种方法同时适用于NSMutableAttributedStringNSAttributedString

[NSAttrStr1, NSAttrStr2].joinWith(separator: "")

另一种方法是...

var full = NSAttrStr1 + NSAttrStr2 + NSAttrStr3

以及:

var full = NSMutableAttributedString(string: "hello ")
// NSAttrStr1 == 1

full += NSAttrStr1 // "hello 1"       
full += " world"   // "hello 1 world"

您可以使用以下扩展名执行此操作:

// works with NSAttributedString and NSMutableAttributedString!
public extension NSAttributedString {
    static func + (left: NSAttributedString, right: NSAttributedString) -> NSAttributedString {
        let leftCopy = NSMutableAttributedString(attributedString: left)
        leftCopy.append(right)
        return leftCopy
    }
    
    static func + (left: NSAttributedString, right: String) -> NSAttributedString {
        let leftCopy = NSMutableAttributedString(attributedString: left)
        let rightAttr = NSMutableAttributedString(string: right)
        leftCopy.append(rightAttr)
        return leftCopy
    }
    
    static func + (left: String, right: NSAttributedString) -> NSAttributedString {
        let leftAttr = NSMutableAttributedString(string: left)
        leftAttr.append(right)
        return leftAttr
    }
}

public extension NSMutableAttributedString {
    static func += (left: NSMutableAttributedString, right: String) -> NSMutableAttributedString {
        let rightAttr = NSMutableAttributedString(string: right)
        left.append(rightAttr)
        return left
    }
    
    static func += (left: NSMutableAttributedString, right: NSAttributedString) -> NSMutableAttributedString {
        left.append(right)
        return left
    }
}
beq87vna

beq87vna6#

如果您使用的是Cocoapods,除了上述两种方法之外,还有一种方法可以避免代码的易变性,那就是在NSAttributedString上使用优秀的NSAttributedString+CCLFormat类别,这样您就可以编写如下代码:

NSAttributedString *first = ...;
NSAttributedString *second = ...;
NSAttributedString *combined = [NSAttributedString attributedStringWithFormat:@"%@%@", first, second];

当然,它只是在幕后使用NSMutableAttributedString
它还有一个额外的优势,那就是它是一个成熟的格式化函数--因此它可以做的不仅仅是将字符串附加在一起。

lp0sw83n

lp0sw83n7#

// Immutable approach
// class method

+ (NSAttributedString *)stringByAppendingString:(NSAttributedString *)append toString:(NSAttributedString *)string {
  NSMutableAttributedString *result = [string mutableCopy];
  [result appendAttributedString:append];
  NSAttributedString *copy = [result copy];
  return copy;
}

//Instance method
- (NSAttributedString *)stringByAppendingString:(NSAttributedString *)append {
  NSMutableAttributedString *result = [self mutableCopy];
  [result appendAttributedString:append];
  NSAttributedString *copy = [result copy];
  return copy;
}
7d7tgy0s

7d7tgy0s8#

您可以尝试SwiftyFormat它使用以下语法

let format = "#{{user}} mentioned you in a comment. #{{comment}}"
let message = NSAttributedString(format: format,
                                 attributes: commonAttributes,
                                 mapping: ["user": attributedName, "comment": attributedComment])
ny6fqffe

ny6fqffe9#

private var fillAttributes:[NSMutableAttributedString.Key : Any]? = nil

fontAttributes = [.foregroundColor : SKColor.red,
                  .strokeWidth : 0.0,
                  .font : CPFont(name: "Verdana-Bold",
                  .size : 50,]

fontAttributes.updateValue(SKColor.green, forKey: .foregroundColor)

相关问题