我想在我的NSAttributedString(修剪它)的前面和末尾骑上白色空间。我不能简单地将其转换为字符串并进行修剪,因为其中有图像(附件)。我该怎么做?
创建NSAttributedString
的扩展,如下所示。
extension NSAttributedString {
public func attributedStringByTrimmingCharacterSet(charSet: NSCharacterSet) -> NSAttributedString {
let modifiedString = NSMutableAttributedString(attributedString: self)
modifiedString.trimCharactersInSet(charSet)
return NSAttributedString(attributedString: modifiedString)
}
}
extension NSMutableAttributedString {
public func trimCharactersInSet(charSet: NSCharacterSet) {
var range = (string as NSString).rangeOfCharacterFromSet(charSet)
// Trim leading characters from character set.
while range.length != 0 && range.location == 0 {
replaceCharactersInRange(range, withString: "")
range = (string as NSString).rangeOfCharacterFromSet(charSet)
}
// Trim trailing characters from character set.
range = (string as NSString).rangeOfCharacterFromSet(charSet, options: .BackwardsSearch)
while range.length != 0 && NSMaxRange(range) == length {
replaceCharactersInRange(range, withString: "")
range = (string as NSString).rangeOfCharacterFromSet(charSet, options: .BackwardsSearch)
}
}
}
并在viewController中使用你想要使用的地方。像这样
let attstring = NSAttributedString(string: "this is test message. Please wait. ")
let result = attstring.attributedStringByTrimmingCharacterSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
这甚至可以在文本中使用表情符号
extension NSAttributedString {
/** Will Trim space and new line from start and end of the text */
public func trimWhiteSpace() -> NSAttributedString {
let invertedSet = CharacterSet.whitespacesAndNewlines.inverted
let startRange = string.utf16.description.rangeOfCharacter(from: invertedSet)
let endRange = string.utf16.description.rangeOfCharacter(from: invertedSet, options: .backwards)
guard let startLocation = startRange?.upperBound, let endLocation = endRange?.lowerBound else {
return NSAttributedString(string: string)
}
let location = string.utf16.distance(from: string.startIndex, to: startLocation) - 1
let length = string.utf16.distance(from: startLocation, to: endLocation) + 2
let range = NSRange(location: location, length: length)
return attributedSubstring(from: range)
}
}
用法
let attributeString = NSAttributedString(string: "\n\n\n Hi 👋 👩👩👧👩👩👦👦👩👩👧👧👨👨👦👩👦👨👨👧👧👨👨👦👦👨👨👧👦👩👧👦👩👦👦👩👧👧👨👦 buddy. ")
let result = attributeString.trimWhiteSpace().string // Hi 👋 👩👩👧👩👩👦👦👩👩👧👧👨👨👦👩👦👨👨👧👧👨👨👦👦👨👨👧👦👩👧👦👩👦👦👩👧👧👨👦 buddy.
我做了一个快速的3实现,以防万一有人感兴趣:
/**
Trim an attributed string. Can for example be used to remove all leading and trailing spaces and line breaks.
*/
public func attributedStringByTrimmingCharactersInSet(set: CharacterSet) -> NSAttributedString {
let invertedSet = set.inverted
let rangeFromStart = string.rangeOfCharacter(from: invertedSet)
let rangeFromEnd = string.rangeOfCharacter(from: invertedSet, options: .backwards)
if let startLocation = rangeFromStart?.upperBound, let endLocation = rangeFromEnd?.lowerBound {
let location = string.distance(from: string.startIndex, to: startLocation) - 1
let length = string.distance(from: startLocation, to: endLocation) + 2
let newRange = NSRange(location: location, length: length)
return self.attributedSubstring(from: newRange)
} else {
return NSAttributedString()
}
}
斯威夫特4及以上
extension NSMutableAttributedString {
func trimmedAttributedString() -> NSAttributedString {
let invertedSet = CharacterSet.whitespacesAndNewlines.inverted
let startRange = string.rangeOfCharacter(from: invertedSet)
let endRange = string.rangeOfCharacter(from: invertedSet, options: .backwards)
guard let startLocation = startRange?.upperBound, let endLocation = endRange?.lowerBound else {
return NSAttributedString(string: string)
}
let location = string.distance(from: string.startIndex, to: startLocation) - 1
let length = string.distance(from: startLocation, to: endLocation) + 2
let range = NSRange(location: location, length: length)
return attributedSubstring(from: range)
}
}
使用:
let string = "This is string with some space in the end. "
let attributedText = NSMutableAttributedString(string: string).trimmedAttributedString()
Swift 3.2版本:
extension NSAttributedString {
public func trimmingCharacters(in characterSet: CharacterSet) -> NSAttributedString {
let modifiedString = NSMutableAttributedString(attributedString: self)
modifiedString.trimCharacters(in: characterSet)
return NSAttributedString(attributedString: modifiedString)
}
}
extension NSMutableAttributedString {
public func trimCharacters(in characterSet: CharacterSet) {
var range = (string as NSString).rangeOfCharacter(from: characterSet)
// Trim leading characters from character set.
while range.length != 0 && range.location == 0 {
replaceCharacters(in: range, with: "")
range = (string as NSString).rangeOfCharacter(from: characterSet)
}
// Trim trailing characters from character set.
range = (string as NSString).rangeOfCharacter(from: characterSet, options: .backwards)
while range.length != 0 && NSMaxRange(range) == length {
replaceCharacters(in: range, with: "")
range = (string as NSString).rangeOfCharacter(from: characterSet, options: .backwards)
}
}
}
以下代码适用于您的要求。
var attString: NSAttributedString = NSAttributedString(string: " this is att string")
let trimmedString = attString.string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())