How to Concatenate Nsattributedstrings

How can I concatenate NSAttributedStrings?

I'd recommend you use a single mutable attributed string a @Linuxios suggested, and here's another example of that:

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];

However, just for the sake of getting all the options out there, you could also create a single mutable attributed string, made from a formatted NSString containing the input strings already put together. You could then use addAttributes: range: to add the attributes after the fact to the ranges containing the input strings. I recommend the former way though.

How to concate two NSAttributed String in swift?

     var attrStr1: NSAttributedString = NSAttributedString(data: "
<b>Welcome</b>".dataUsingEncoding(NSUnicodeStringEncoding,
allowLossyConversion: true)!,options: [ NSDocumentTypeDocumentAttribute:
NSHTMLTextDocumentType],documentAttributes: nil,error: nil)!

var attrStr2: NSAttributedString = NSAttributedString(data: "
<i>User</i>".dataUsingEncoding(NSUnicodeStringEncoding,
allowLossyConversion: true)!,options: [ NSDocumentTypeDocumentAttribute:
NSHTMLTextDocumentType],documentAttributes: nil,error: nil)!

Concatenate this strings :

var concate = NSMutableAttributedString(attributedString: attrStr1)
concate.appendAttributedString(attrStr2)

self.label.text = concate

how to append Attributed Text String with Attributed String in Swift

Use NSMutableAttributedString to achieve that.

Example

Swift 5

let yourAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15)]
let yourOtherAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 25)]

let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)

partOne.append(partTwo)

Swift 3

let yourAttributes = [NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: UIFont.systemFont(ofSize: 15)]
let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: UIFont.systemFont(ofSize: 25)]

let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)

let combination = NSMutableAttributedString()

combination.append(partOne)
combination.append(partTwo)

combination represents your final string which contains both formattings provided by yourAttributes and yourOtherAttributes

Even older

let yourAttributes = [NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont.systemFontOfSize(15)]
let yourOtherAttributes = [NSForegroundColorAttributeName: UIColor.redColor(), NSFontAttributeName: UIFont.systemFontOfSize(25)]

let partOne = NSMutableAttributedString(string: "This is an example ", attributes: yourAttributes)
let partTwo = NSMutableAttributedString(string: "for the combination of Attributed String!", attributes: yourOtherAttributes)

let combination = NSMutableAttributedString()

combination.appendAttributedString(partOne)
combination.appendAttributedString(partTwo)

Is there joinWithSeparator for attributed strings

Swift 5:

import Foundation

extension Sequence where Iterator.Element == NSAttributedString {
func joined(with separator: NSAttributedString) -> NSAttributedString {
return self.reduce(NSMutableAttributedString()) {
(r, e) in
if r.length > 0 {
r.append(separator)
}
r.append(e)
return r
}
}

func joined(with separator: String = "") -> NSAttributedString {
return self.joined(with: NSAttributedString(string: separator))
}
}

Swift 4:

import Foundation

extension SequenceType where Generator.Element: NSAttributedString {
func joinWithSeparator(separator: NSAttributedString) -> NSAttributedString {
var isFirst = true
return self.reduce(NSMutableAttributedString()) {
(r, e) in
if isFirst {
isFirst = false
} else {
r.appendAttributedString(separator)
}
r.appendAttributedString(e)
return r
}
}

func joinWithSeparator(separator: String) -> NSAttributedString {
return joinWithSeparator(NSAttributedString(string: separator))
}
}

Append attributed string to attributed text of UITextField and keep all it's previous colors and attributes

I've got your code working now. I think something happens before or after your main section and resets attribute color for range.

And heres some idea of how you can refactor it:

@IBAction func buttonClick(_ sender: UIButton) {

if let completionTextHolderText = completionTextHolderText,
let attrStr = userTextField.attributedText {

let newMutableString = attrStr.mutableCopy() as! NSMutableAttributedString

newMutableString.append(
createAttributed(string: completionTextHolderText+" ", with: .blue)
)

userTextField.attributedText = newMutableString
}
}

private func createAttributed(string: String, with color: UIColor) -> NSAttributedString {
return NSAttributedString(string: string, attributes: [NSForegroundColorAttributeName : color])
}

Appending NSAttributedStrings returns an error

Seems your array contains NSString objects. NSAttributedString is not a subclass of NSString or vice versa. Both of them inherit from NSObject.

Before you append, try to create an instance of NSAttributedString with the method initWithString and pass str as the argument.

NSAttributedString *attributedString = [NSAttributedString initWithString:str];
[result appendAttributedString:attributedString];

And also the for loop needs to be updated:

for (NSString *str in myStrings) {
}


Related Topics



Leave a reply



Submit