NsMutableAttributed string swift - ios

I am working on editor app in swift.
For editor I use an attributed string and I am getting a random error when cut and paste text.
This is the error :
-[NSBigMutableString characterAtIndex:]: Index 3369 out of bounds; string length 3367'
I am using this code for attributed string .
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if (text.characters.count == 0 && range.length == 1) {
return true
}
if !isBold {
let size = CGFloat(Float(lblFontSize.text!)!)
let boldFont: UIFont = UIFont(name: "MinwalaNastaleeq-checking", size: size)!
let boldAttr: [String : AnyObject] = [
NSFontAttributeName : boldFont,
NSParagraphStyleAttributeName : titleParagraphStyle,
NSForegroundColorAttributeName : textColor
]
let attributedText: NSMutableAttributedString = NSMutableAttributedString(string: text, attributes: boldAttr)
textView.replaceRange(textView.selectedTextRange!, withText: text)
let textViewText: NSMutableAttributedString = NSMutableAttributedString(attributedString: textView.attributedText)
textView.attributedText = textViewText
return false
}
return true
}
Thanks in advance

Related

Kern Attribute cause Performance slow issue in UITextview

I want to set width of each characters when user typing. I use NSAttributeString with key .Kern that works well but when the text length is bigger about 1000+, I touch on screen to place cursor position in front of first line and typing new characters that cause Performance issue, too slow and lag.
// Initialize once and reuse every time
lazy var paragraphStyle: NSMutableParagraphStyle = {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byWordWrapping
paragraphStyle.alignment = .left
return paragraphStyle
}()
// Initialize once and reuse every time
lazy var alignTextAttributes: [NSAttributedString.Key: Any] = {
return [
.kern : 30,
.paragraphStyle : paragraphStyle,
.foregroundColor : UIColor.black,
]
}()
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
// If I want to set each characters with different kern values, how can I do that?
alignTextAttributes[.kern] = textView.text.count.isMultiple(of: 2) ? 30 : 10
// This is the key part
textView.typingAttributes = alignTextAttributes
// Allow system to control typing
return true
}
Here's what you can try -
// 4 (or 6 or 8) spaces (depending on font size)
let padding: String = " "
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
var cursorLocation = textView.selectedRange.location
if text.isEmpty {
let deleteRange = NSRange(
location: max(0, range.location - padding.count),
length: range.length + padding.count
)
textView.textStorage.replaceCharacters(in: deleteRange, with: text)
cursorLocation -= deleteRange.length
}
else {
let insertRange = NSRange(
location: range.location,
length: range.length + padding.count
)
textView.textStorage.replaceCharacters(in: insertRange, with: text.appending(padding))
cursorLocation += insertRange.length
}
textView.selectedRange.location = cursorLocation
return false
}

[NSConcreteTextStorage attribute:atIndex:effectiveRange:]: Range or index out of bounds error in NSMutableAttributedString

I am trying to create an attributed string in which there is a Link appended at the end of the string :
func addMoreAndLessFunctionality (textView:UITextView){
if textView.text.characters.count >= 120
{
let lengthOfString = 255
var abc : String = (somelongStringInitiallyAvailable as NSString).substringWithRange(NSRange(location: 0, length: lengthOfString))
abc += " ...More"
textView.text = abc
let attribs = [NSForegroundColorAttributeName: StyleKit.appDescriptionColor, NSFontAttributeName: StyleKit.appDescriptionFont]
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: abc, attributes: attribs)
attributedString.addAttribute(NSLinkAttributeName, value: " ...More", range: NSRange(location:255, length: 8))
textView.attributedText = attributedString
textView.textContainer.maximumNumberOfLines = 3;
}
}
What I am trying to achieve here is that if characters in text view's text more than 255, it should show the "...More" link in text view which is tapable, on tap I am already able get the delegate "shouldInteractWithUrl" called, where I am increasing the no of lines in text view, and also change the text of link to "...Less". On tap of Less I again call this same method so that it can truncate again. :
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool
{
print("textview should interact with URl")
let textTapped = textView.text[textView.text.startIndex.advancedBy(characterRange.location)..<textView.text.endIndex]
if textTapped == " ...More"{
var abc : String = (self.contentDetailItemManaged?.whatsnewDesc)!
abc += " ...Less"
textView.textContainer.maximumNumberOfLines = 0
let attribs = [NSForegroundColorAttributeName: StyleKit.appDescriptionColor, NSFontAttributeName: StyleKit.appDescriptionFont]
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: abc, attributes: attribs)
attributedString.addAttribute(NSLinkAttributeName, value: " ...Less", range: NSRange(location:abc.characters.count-8 , length: 8))
textView.attributedText = attributedString
}
else if textTapped == " ...Less"{
textView.attributedText = nil
textView.text = somelongStringInitiallyAvailable
self.addMoreAndLessFunctionality(textView)
}
return true
}
Now the problem, when the 1st method is called for the first time (it is called after I have set the textview's text), it works fine, but after clicking on "...More", textview expands normally, "...More" changes to " ...Less". And when " ...Less" is tapped, It crashes and exception occurs :
[NSConcreteTextStorage attribute:atIndex:effectiveRange:]: Range or index out of bounds'
any help would be greatly appreciated. (Also string "...More" has a space in beginning, so total 8 chars, i may have missed this space while typing above code)
Thanks :)
return false in func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool

AttributedText in TextView while typing [duplicate]

This question already has answers here:
Replace UITextViews text with attributed string
(4 answers)
Closed 6 years ago.
I have a textView, and I am trying to give it an attributed text. I tried achieving it inside shouldChangeTextInRange, but it crashes for range out of index.
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if myTextView {
textView.attributedText = addAttributedText(1, text: text, fontsize: 13)
let newText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)
let numberOfChars = newText.characters.count
return numberOfChars < 20
}
return true
}
func addAttributedText(spacing:CGFloat, text:String, fontsize: CGFloat) -> NSMutableAttributedString {
let attributedString = NSMutableAttributedString(string: text, attributes: [NSFontAttributeName:UIFont(
name: "Font",
size: fontsize)!])
attributedString.addAttribute(NSKernAttributeName, value: spacing, range: NSMakeRange(0, text.characters.count))
return attributedString
}
I tried adding attributedString with empty text to textView in viewDidLoad, but that doesn't help. That's why I thought it would be appropriate to do it on shouldChangeTextInRange
(Please note that my addAttributedText method works perfectly for other textviews)
If I use this, in one character type-in, it writes 2x and crashes. What is the right way of handling that kind of converting textView's text to attributed text that is being typed.
Here is the code that I tried to convert from the link above, it might have bugs, but I hope it will be able to help you.
func formatTextInTextView(textView: UITextView)
{
textView.scrollEnabled = false
var selectedRange: NSRange = textView.selectedRange
var text: String = textView.text!
// This will give me an attributedString with the base text-style
var attributedString: NSMutableAttributedString = NSMutableAttributedString(string: text)
var error: NSError? = nil
var regex: NSRegularExpression = NSRegularExpression.regularExpressionWithPattern("#(\\w+)", options: 0, error: error!)
var matches: [AnyObject] = regex.matchesInString(text, options: 0, range: NSMakeRange(0, text.length))
for match: NSTextCheckingResult in matches {
var matchRange: NSRange = match.rangeAtIndex(0)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: matchRange)
}
textView.attributedText = attributedString
textView.selectedRange = selectedRange
textView.scrollEnabled = true
}
EDIT: didn't see that in the original post there was a Swift answer, here is the link: stackoverflow.com/a/35842523/1226963

Make part of a UILabel bold in Swift

I have a UILabel I've made programmatically as:
var label = UILabel()
I've then declared some styling for the label, including a font, such as:
label.frame = CGRect(x: 20, y: myHeaderView.frame.height / 2, width: 300, height: 30)
label.font = UIFont(name: "Typo GeoSlab Regular Demo", size: 15)
label.textColor = UIColor(hue: 0/360, saturation: 0/100, brightness: 91/100, alpha: 1)
The first part of the label will always read : "Filter:" then followed by another part of the string, for example, "Most popular"
I would like the word filter to be in bold, so the whole thing would look like:
Filter: Most popular
I want to simplest way of creating this effect. I've been searching the internet for how to achieve this and there are so many ways, some which just look like pages of code. And most of it seems to be in Objective-C. I would like it in Swift please :)
I don't know if i'm on the right lines, but is this what NSRange can help achieve?
Update
I use a series of if statements to change my label variable. Such as:
if indexArray == 1 {
label.text = "Filter: Film name"
} else if indexArray == 2 {
label.text = "Filter: Most popular"
} else if indexArray == 3 {
label.text = "Filter: Star rating"
}
You will want to use attributedString which allows you to style parts of a string etc. This can be done like this by having two styles, one normal, one bold, and then attaching them together:
let boldText = "Filter:"
let attrs = [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 15)]
let attributedString = NSMutableAttributedString(string:boldText, attributes:attrs)
let normalText = "Hi am normal"
let normalString = NSMutableAttributedString(string:normalText)
attributedString.append(normalString)
When you want to assign it to a label:
label.attributedText = attributedString
You can use NSMutableAttributedString and NSAttributedString to create customized string. The function below makes given boldString bold in given string.
Swift 3
func attributedText(withString string: String, boldString: String, font: UIFont) -> NSAttributedString {
let attributedString = NSMutableAttributedString(string: string,
attributes: [NSFontAttributeName: font])
let boldFontAttribute: [String: Any] = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: font.pointSize)]
let range = (string as NSString).range(of: boldString)
attributedString.addAttributes(boldFontAttribute, range: range)
return attributedString
}
Example usage
authorLabel.attributedText = attributedText(withString: String(format: "Author : %#", user.name), boldString: "Author", font: authorLabel.font)
Swift 4
func attributedText(withString string: String, boldString: String, font: UIFont) -> NSAttributedString {
let attributedString = NSMutableAttributedString(string: string,
attributes: [NSAttributedStringKey.font: font])
let boldFontAttribute: [NSAttributedStringKey: Any] = [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: font.pointSize)]
let range = (string as NSString).range(of: boldString)
attributedString.addAttributes(boldFontAttribute, range: range)
return attributedString
}
Swift 4.2 and 5
func attributedText(withString string: String, boldString: String, font: UIFont) -> NSAttributedString {
let attributedString = NSMutableAttributedString(string: string,
attributes: [NSAttributedString.Key.font: font])
let boldFontAttribute: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: font.pointSize)]
let range = (string as NSString).range(of: boldString)
attributedString.addAttributes(boldFontAttribute, range: range)
return attributedString
}
Result:
Swift 4.2 & 5.0:
First off we create a protocol that UILabel, UITextField and UITextView can adopt.
public protocol ChangableFont: AnyObject {
var rangedAttributes: [RangedAttributes] { get }
func getText() -> String?
func set(text: String?)
func getAttributedText() -> NSAttributedString?
func set(attributedText: NSAttributedString?)
func getFont() -> UIFont?
func changeFont(ofText text: String, with font: UIFont)
func changeFont(inRange range: NSRange, with font: UIFont)
func changeTextColor(ofText text: String, with color: UIColor)
func changeTextColor(inRange range: NSRange, with color: UIColor)
func resetFontChanges()
}
We want to be able to add multiple changes to our text, therefore we create the rangedAttributes property. It's a custom struct that holds attributes and the range in which they are applied.
public struct RangedAttributes {
public let attributes: [NSAttributedString.Key: Any]
public let range: NSRange
public init(_ attributes: [NSAttributedString.Key: Any], inRange range: NSRange) {
self.attributes = attributes
self.range = range
}
}
Another problem is that UILabel its font property is strong and UITextField its font property is weak/optional. To make them both work with our ChangableFont protocol we include the getFont() -> UIFont? method. This also counts for UITextView its text and attributedText properties. That's why we implement the getter and setter methods for them as well.
extension UILabel: ChangableFont {
public func getText() -> String? {
return text
}
public func set(text: String?) {
self.text = text
}
public func getAttributedText() -> NSAttributedString? {
return attributedText
}
public func set(attributedText: NSAttributedString?) {
self.attributedText = attributedText
}
public func getFont() -> UIFont? {
return font
}
}
extension UITextField: ChangableFont {
public func getText() -> String? {
return text
}
public func set(text: String?) {
self.text = text
}
public func getAttributedText() -> NSAttributedString? {
return attributedText
}
public func set(attributedText: NSAttributedString?) {
self.attributedText = attributedText
}
public func getFont() -> UIFont? {
return font
}
}
extension UITextView: ChangableFont {
public func getText() -> String? {
return text
}
public func set(text: String?) {
self.text = text
}
public func getAttributedText() -> NSAttributedString? {
return attributedText
}
public func set(attributedText: NSAttributedString?) {
self.attributedText = attributedText
}
public func getFont() -> UIFont? {
return font
}
}
Now we can go ahead and create the default implementation for UILabel, UITextField and UITextView by extending our protocol.
public extension ChangableFont {
var rangedAttributes: [RangedAttributes] {
guard let attributedText = getAttributedText() else {
return []
}
var rangedAttributes: [RangedAttributes] = []
let fullRange = NSRange(
location: 0,
length: attributedText.string.count
)
attributedText.enumerateAttributes(
in: fullRange,
options: []
) { (attributes, range, stop) in
guard range != fullRange, !attributes.isEmpty else { return }
rangedAttributes.append(RangedAttributes(attributes, inRange: range))
}
return rangedAttributes
}
func changeFont(ofText text: String, with font: UIFont) {
guard let range = (self.getAttributedText()?.string ?? self.getText())?.range(ofText: text) else { return }
changeFont(inRange: range, with: font)
}
func changeFont(inRange range: NSRange, with font: UIFont) {
add(attributes: [.font: font], inRange: range)
}
func changeTextColor(ofText text: String, with color: UIColor) {
guard let range = (self.getAttributedText()?.string ?? self.getText())?.range(ofText: text) else { return }
changeTextColor(inRange: range, with: color)
}
func changeTextColor(inRange range: NSRange, with color: UIColor) {
add(attributes: [.foregroundColor: color], inRange: range)
}
private func add(attributes: [NSAttributedString.Key: Any], inRange range: NSRange) {
guard !attributes.isEmpty else { return }
var rangedAttributes: [RangedAttributes] = self.rangedAttributes
var attributedString: NSMutableAttributedString
if let attributedText = getAttributedText() {
attributedString = NSMutableAttributedString(attributedString: attributedText)
} else if let text = getText() {
attributedString = NSMutableAttributedString(string: text)
} else {
return
}
rangedAttributes.append(RangedAttributes(attributes, inRange: range))
rangedAttributes.forEach { (rangedAttributes) in
attributedString.addAttributes(
rangedAttributes.attributes,
range: rangedAttributes.range
)
}
set(attributedText: attributedString)
}
func resetFontChanges() {
guard let text = getText() else { return }
set(attributedText: NSMutableAttributedString(string: text))
}
}
With in the default implementation I use a little helper method for getting the NSRange of a substring.
public extension String {
func range(ofText text: String) -> NSRange {
let fullText = self
let range = (fullText as NSString).range(of: text)
return range
}
}
We're done! You can now change parts of the text its font and text color.
titleLabel.text = "Welcome"
titleLabel.font = UIFont.systemFont(ofSize: 70, weight: .bold)
titleLabel.textColor = UIColor.black
titleLabel.changeFont(ofText: "lc", with: UIFont.systemFont(ofSize: 60, weight: .light))
titleLabel.changeTextColor(ofText: "el", with: UIColor.blue)
titleLabel.changeTextColor(ofText: "co", with: UIColor.red)
titleLabel.changeTextColor(ofText: "m", with: UIColor.green)
Swift 4 alternative:
let attrs = [NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 14)]
let attributedString = NSMutableAttributedString(string: "BOLD TEXT", attributes:attrs)
let normalString = NSMutableAttributedString(string: "normal text")
attributedString.append(normalString)
myLabel.attributedText = attributedString
You can directly do on String if you prefer:
extension String {
func withBoldText(text: String, font: UIFont? = nil) -> NSAttributedString {
let _font = font ?? UIFont.systemFont(ofSize: 14, weight: .regular)
let fullString = NSMutableAttributedString(string: self, attributes: [NSAttributedString.Key.font: _font])
let boldFontAttribute: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: _font.pointSize)]
let range = (self as NSString).range(of: text)
fullString.addAttributes(boldFontAttribute, range: range)
return fullString
}}
Usage:
label.attributeString = "my full string".withBoldText(text: "full")
for the ones who prefer extensions
Swift 5.0
/// will set a regual and a bold text in the same label
public func setRegualAndBoldText(regualText: String,
boldiText: String) {
let attrs = [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: font.pointSize)]
let regularString = NSMutableAttributedString(string: regualText)
let boldiString = NSMutableAttributedString(string: boldiText, attributes:attrs)
regularString.append(boldiString)
attributedText = regularString
}
and use:
label.setRegualAndBoldText(regualText: "height: ", boldiText: "1.65 :(")
Just sharing my own quite-flexible implementation in Swift 4.0. Cause there are some requirements, like mine currently, that you need to set not only bold but italic the part of a label's text.
import UIKit
extension UILabel {
/** Sets up the label with two different kinds of attributes in its attributed text.
* #params:
* - primaryString: the normal attributed string.
* - secondaryString: the bold or highlighted string.
*/
func setAttributedText(primaryString: String, textColor: UIColor, font: UIFont, secondaryString: String, secondaryTextColor: UIColor, secondaryFont: UIFont) {
let completeString = "\(primaryString) \(secondaryString)"
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let completeAttributedString = NSMutableAttributedString(
string: completeString, attributes: [
.font: font,
.foregroundColor: textColor,
.paragraphStyle: paragraphStyle
]
)
let secondStringAttribute: [NSAttributedStringKey: Any] = [
.font: secondaryFont,
.foregroundColor: secondaryTextColor,
.paragraphStyle: paragraphStyle
]
let range = (completeString as NSString).range(of: secondaryString)
completeAttributedString.addAttributes(secondStringAttribute, range: range)
self.attributedText = completeAttributedString
}
}
If you know which character place values you want to bold I created a function which takes ranges of characters and optional fonts (use nil if you just want to use the standard system font of size 12), and returns an NSAttributedString which you can attach to a label as its attributed text. I wanted to bolden the 0th, 10th, 22-23rd, 30th and 34th characters of my string so i used [[0,0], [10,10], [22,23], [30,30], [34,34]] for my boldCharactersRanges value.
Usage:
func boldenParts(string: String, boldCharactersRanges: [[Int]], regularFont: UIFont?, boldFont: UIFont?) -> NSAttributedString {
let attributedString = NSMutableAttributedString(string: string, attributes: [NSAttributedString.Key.font: regularFont ?? UIFont.systemFont(ofSize: 12)])
let boldFontAttribute: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: boldFont ?? UIFont.boldSystemFont(ofSize: regularFont?.pointSize ?? UIFont.systemFontSize)]
for range in boldCharactersRanges {
let currentRange = NSRange(location: range[0], length: range[1]-range[0]+1)
attributedString.addAttributes(boldFontAttribute, range: currentRange)
}
return attributedString
}
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel()
label.frame = CGRect(x: 0, y: 0, width: 180, height: 50)
label.numberOfLines = 0
label.center = view.center
let text = "Under the pillow is a vogue article"
let secretMessage = boldenParts(string: text, boldCharactersRanges: [[0,0], [10,10], [22,23], [30,30], [34,34]], regularFont: UIFont(name: "Avenir", size: 15), boldFont: UIFont(name: "Avenir-Black", size: 15))
label.attributedText = secretMessage
view.addSubview(label)
}
Swift 4.0 solution
let font = UIFont.systemFont(ofSize: 14)
func boldSearchResult(searchString: String, resultString: String) -> NSMutableAttributedString {
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: resultString)
guard let regex = try? NSRegularExpression(pattern: searchString.lowercased(), options: []) else {
return attributedString
}
let range: NSRange = NSMakeRange(0, resultString.count)
regex.enumerateMatches(in: resultString.lowercased(), options: [], range: range) { (textCheckingResult, matchingFlags, stop) in
guard let subRange = textCheckingResult?.range else {
return
}
attributedString.addAttributes([NSAttributedString.Key.font : font], range: subRange)
}
return attributedString
}

How can I change overflowed characters color of my UITextView?

I would like to have the follow result:
Dream result
I tried to use this code, but no success:
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if range.location > 159 {
let attributedString = NSMutableAttributedString(string: bioTextView.text)
let range2 = (bioTextView.text as NSString).rangeOfString(bioTextView.text)
attributedString.addAttributes([NSForegroundColorAttributeName: UIColor.flatRedColor(), NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 14)!], range: range2)
bioTextView.attributedText = attributedString
}
}
I saw in this post: Finding index of character in Swift String
That I should not convert to NSString, because can bug with emoji 😐
PS: Sorry about my English, I'm still learning.
Please use following function to achieve your desired result.
Declare max length of text that you want to show in default color, other than that text will be red.
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
let maxLength = 159
if range.location > maxLength {
let index = textView.text.startIndex.advancedBy(maxLength)
let mainString = textView.text.substringToIndex(index)
let mainAttributedString = NSMutableAttributedString (string: mainString, attributes: [NSForegroundColorAttributeName: UIColor.darkGrayColor(), NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 14)!])
let redAttributedString = NSAttributedString(string: textView.text.substringFromIndex(index), attributes: [NSForegroundColorAttributeName: UIColor.redColor(), NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 14)!])
mainAttributedString.appendAttributedString(redAttributedString)
textView.attributedText = mainAttributedString;
}
}

Resources