I'm having a custom NSLayoutManager with these two methods overwritten:
override func drawGlyphs(forGlyphRange glyphsToShow: NSRange, at origin: CGPoint) {
super.drawGlyphs(forGlyphRange: glyphsToShow, at: origin)
let characterRange = self.characterRange(forGlyphRange: glyphsToShow, actualGlyphRange: nil)
textStorage?.enumerateAttribute(.blur, in: characterRange, options: .longestEffectiveRangeNotRequired, using: { (value, subrange, _) in
guard let key = value as? String, !key.isEmpty else { return }
let blurGlyphRange = glyphRange(forCharacterRange: subrange, actualCharacterRange: nil)
drawBlur(forGlyphRange: blurGlyphRange)
textStorage?.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], range: blurGlyphRange)
})
}
private func drawBlur(forGlyphRange tokenGlypeRange: NSRange) {
guard let textContainer = textContainer(forGlyphAt: tokenGlypeRange.location, effectiveRange: nil) else { return }
let withinRange = NSRange(location: NSNotFound, length: 0)
enumerateEnclosingRects(forGlyphRange: tokenGlypeRange, withinSelectedGlyphRange: withinRange, in: textContainer) { (rect, _) in
let blurRect = rect.offsetBy(dx: self.textContainerOriginOffset.width, dy: self.textContainerOriginOffset.height)
UIColor.red.setFill()
UIBezierPath(roundedRect: blurRect, cornerRadius: 4).fill()
}
Everything works fine except when I set the UITextView isScrollingEnabled on false
I enter an endless loop caused by the textStorage enumerateAttribute method in drawGlyphs.
I don't understand why this happens and also I don't know how to prevent this.
Someone who knows more about this?
EDIT
If I remove the textStorage addAttributes with the foregroundColor then it works. So that's causing the loop for some reason.
I found the problem why it comes in and endless loop. The textstorage updates/add the attribute and then notifies the layoutmanager again.
Solution is to create your own textstorage like this:
class CustomTextStorage: NSTextStorage {
private let backingStore = NSMutableAttributedString()
override var string: String {
return backingStore.string
}
override init() {
super.init()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func attributes(at location: Int, effectiveRange range: NSRangePointer?) -> [NSAttributedString.Key: Any] {
return backingStore.attributes(at: location, effectiveRange: range)
}
override func replaceCharacters(in range: NSRange, with str: String) {
beginEditing()
backingStore.replaceCharacters(in: range, with:str)
edited(.editedCharacters, range: range, changeInLength: (str as NSString).length - range.length)
endEditing()
}
override func setAttributes(_ attrs: [NSAttributedString.Key: Any]?, range: NSRange) {
beginEditing()
backingStore.setAttributes(attrs, range: range)
if let attrs = attrs, let _ = attrs[.blur] {
backingStore.addAttribute(.foregroundColor, value: UIColor.clear, range: range)
}
edited(.editedAttributes, range: range, changeInLength: 0)
endEditing()
}
}
and remove the line under the draw method in the layoutmanager:
textStorage?.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], range: blurGlyphRange)
Related
The build-in keys all works well, it will follow the existing text after edit (insert/replace/paste)
But when I doing it with custom NSAttributedString.Key, the attribute doesn't follow.
tagtest is a custom NSAttributedString.Key
extension NSAttributedString.Key {
static let drawTag: NSAttributedString.Key = .init("drawTag")
}
And I draw it by custom NSLayoutManager (similar style with drawing background, but I do need a custom one)
func drawTag(forGlyphRange glyphRange: NSRange,
fillColor:UIColor,
lineFragmentRect lineRect: CGRect,
lineFragmentGlyphRange lineGlyphRange: NSRange,
containerOrigin: CGPoint) {
if let textContainer = textContainer(forGlyphAt: glyphRange.location, effectiveRange: nil) {
let range = NSIntersectionRange(glyphRange, lineGlyphRange)
let lineRect = self.boundingRect(forGlyphRange: range, in: textContainer)
let path = UIBezierPath(roundedRect: lineRect, cornerRadius: 3)
fillColor.setFill()
path.fill()
}
}
I tried to override the NSTextStorage
The attribute can be read but still missed after edit
override func replaceCharacters(in range: NSRange, with str: String) {
beginEditing()
var str = str
var attrs: [NSAttributedString.Key:Any] = [:]
if range.location < string.count{
var r = range
attrs = attributes(at: range.location, effectiveRange: nil)
str = str + "\((attrs[NSAttributedString.Key.drawTag]))"
}
container.replaceCharacters(in: range, with: NSAttributedString(string: str, attributes: attrs))
//container.addAttributes(attrs, range: NSMakeRange(range.location, str.count))
edited([.editedAttributes, .editedCharacters], range: range, changeInLength: (str as NSString).length - range.length)
endEditing()
}
Or override the UITextViewDelegate
This is the most closest one, but cursor will jump to the end, and there will be error when input texts at end.
func textView(_ uiView: UITextView, shouldChangeTextIn: NSRange, replacementText: String) -> Bool{
let attrs = attributedText.attributes(at: shouldChangeTextIn.location, effectiveRange: nil)
attributedText.replaceCharacters(in: shouldChangeTextIn, with: NSAttributedString(string: replacementText, attributes: attrs))
textView.configure(uiView)
return false
}
I have added a uitextview which is initially non editable. I added a tap gesture which enable the editing to true. In the tap gesture selector I get the word that is being tapped. I have tried a lot many solution but none worked for me as a complete solution. Every solution worked if the textview is not scrolled. But if I scroll the textview the exact word is not retrieved. Here is my code for getting the tapped word:
#objc func handleTap(_ sender: UITapGestureRecognizer) {
notesTextView.isEditable = true
notesTextView.textColor = UIColor.white
if let textView = sender.view as? UITextView {
var pointOfTap = sender.location(in: textView)
print("x:\(pointOfTap.x) , y:\(pointOfTap.y)")
let contentOffsetY = textView.contentOffset.y
pointOfTap.y += contentOffsetY
print("x:\(pointOfTap.x) , y:\(pointOfTap.y)")
word(atPosition: pointOfTap)
}
func word(atPosition: CGPoint) -> String? {
if let tapPosition = notesTextView.closestPosition(to: atPosition) {
if let textRange = notesTextView.tokenizer.rangeEnclosingPosition(tapPosition , with: .word, inDirection: 1) {
let tappedWord = notesTextView.text(in: textRange)
print("Word: \(tappedWord)" ?? "")
return tappedWord
}
return nil
}
return nil
}
EDITED:
Here is the demo project with the problem.
https://github.com/amrit42087/TextViewDemo
The best and easiest way in Swift 4
METHOD 1:
Step 1: Add Tap Gesture on the textview
let tap = UITapGestureRecognizer(target: self, action: #selector(tapResponse(recognizer:)))
textViewTC.addGestureRecognizer(tap)
Step 2: Implement Tap Gesture
#objc func tapResponse(recognizer: UITapGestureRecognizer) {
let location: CGPoint = recognizer.location(in: textViewTC)
let position: CGPoint = CGPoint(x: location.x, y: location.y)
let tapPosition: UITextPosition = textViewTC.closestPosition(to: position)!
guard let textRange: UITextRange = textViewTC.tokenizer.rangeEnclosingPosition(tapPosition, with: UITextGranularity.word, inDirection: 1) else {return}
let tappedWord: String = textViewTC.text(in: textRange) ?? ""
print("tapped word ->", tappedWord)
}
And yes thats it. Go for it.
METHOD 2:
The alternate way is that you can enable links for textview and then set the same as an attribute. Here is an example
var foundRange = attributedString.mutableString.range(of: "Terms of Use") //mention the parts of the attributed text you want to tap and get an custom action
attributedString.addAttribute(NSAttributedStringKey.link, value: termsAndConditionsURL, range: foundRange)
set this attribute text to Textview and textView.delegate = self
Now you just need to handle the response in
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
Hope it helps you. All the best.
You don't need to add the content offset of the text view. When you convert location into a scrollview it will already take its content offset into account.
Removing:
let contentOffsetY = textView.contentOffset.y
pointOfTap.y += contentOffsetY
should work.
Please Try This
//add UITextViewDelegate
let termsAndConditionsURL = "someText"
let privacyURL = "SampleText"
#IBOutlet weak var terms: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
self.terms.delegate = self
// Adding Attributed text to TextView
let str = "By registering, you agree to the Terms and the User Privacy Statement."
let attributedString = NSMutableAttributedString(string: str)
var foundRange = attributedString.mutableString.range(of: "Terms")
attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue, range: foundRange)
attributedString.addAttribute(NSAttributedStringKey.underlineStyle , value: NSUnderlineStyle.styleSingle.rawValue, range: foundRange)
attributedString.addAttribute(NSAttributedStringKey.link, value: termsAndConditionsURL, range: foundRange)
foundRange = attributedString.mutableString.range(of: "User Privacy Statement")
attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue, range: foundRange)
attributedString.addAttribute(NSAttributedStringKey.underlineStyle , value: NSUnderlineStyle.styleSingle.rawValue, range: foundRange)
attributedString.addAttribute(NSAttributedStringKey.link, value: privacyURL, range: foundRange)
terms.attributedText = attributedString
// Do any additional setup after loading the view.
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool
{
if (URL.absoluteString == termsAndConditionsURL)
{
// Perform Terms Action here
} else if (URL.absoluteString == privacyURL)
{
// Perform Terms Action here
}
return false
}
I want NSTextView object to react to Tab key hit by changing NSParagraphStyle spacing. And it does but EXTREMELY slow!!! In fact if I do this changes too quick (hit Tab key too fast), I eventually got glitches that sometimes even lead to crash. Here's the video: https://drive.google.com/open?id=0B4aMQXnlIOvCUXNjWTVXVkR3NHc. And another one: https://drive.google.com/open?id=0B4aMQXnlIOvCUDJjSEN0bFdqQXc
Fragment of code from my NSTextStorage subclass:
override func attributesAtIndex(index: Int, effectiveRange range: NSRangePointer) -> [String : AnyObject] {
return storage.attributesAtIndex(index, effectiveRange: range)
}
override func replaceCharactersInRange(range: NSRange, withString str: String) {
let delta = str.characters.count - range.length
beginEditing()
storage.replaceCharactersInRange(range, withString:str)
edited([.EditedCharacters, .EditedAttributes], range: range, changeInLength: delta)
endEditing()
}
override func setAttributes(attrs: [String : AnyObject]!, range: NSRange) {
beginEditing()
storage.setAttributes(attrs, range: range)
edited(.EditedAttributes, range: range, changeInLength: 0)
endEditing()
}
Fragment of code from my NSTextView subclass:
override func shouldChangeTextInRange(affectedCharRange: NSRange, replacementString: String?) -> Bool {
super.shouldChangeTextInRange(affectedCharRange, replacementString: replacementString)
guard replacementString != nil else { return true }
if replacementString == "\t" {
defer {
let selectedRangesValues = self.selectedRanges
var selectedRanges = [NSRange]()
for value in selectedRangesValues {
selectedRanges.append(value.rangeValue)
}
textController.switchToAltAttributesInRange(selectedRanges)
}
return false
}
return true
}
Fragment of code from my TextController which creates and applies alternative attributes:
func switchToAltAttributesInRange(ranges : [NSRange]) {
// get paragraph indexes from the ranges
var indexes = [Int]()
for range in ranges {
for idx in textStorage.paragraphsInRange(range) {
indexes.append(idx)
}
}
// change attributes for all the paragraphs in those ranges
for index in indexes {
let paragraphRange = textStorage.paragraphRangeAtIndex(index)
let element = elementAtIndex(index)
let altElementType = altElementTypeForElementType(element.type)
// change the attributes
let newAttributes = paragraphAttributesForElement(type: altElementType.rawValue)
self.textStorage.beginUpdates()
self.textStorage.setAttributes(newAttributes, range: paragraphRange)
self.textStorage.endUpdates()
}
}
func paragraphAttributesForElement(type typeString: String) -> [String : AnyObject] {
let elementPreset = elementPresetForType(elementType)
// set font
let font = NSFont (name: elementPreset.font, size: CGFloat(elementPreset.fontSize))!
// set attributes
let elementAttributes = [NSFontAttributeName: font,
NSParagraphStyleAttributeName : paragraphStyleForElementPreset(elementPreset, font: font),
NSForegroundColorAttributeName: NSColor.colorFromHexValue(elementPreset.color),
NSUnderlineStyleAttributeName : elementPreset.underlineStyle,
ElementAttribute.AllCaps.rawValue : elementPreset.allCaps]
return elementAttributes
}
func paragraphStyleForElementPreset(elementPreset : TextElementPreset, font : NSFont) -> NSParagraphStyle {
let sceneParagraphStyle = NSMutableParagraphStyle()
let spacing = elementPreset.spacing
let spacingBefore = elementPreset.spacingBefore
let headIndent = elementPreset.headIndent
let tailIndent = elementPreset.tailIndent
let cFont = CTFontCreateWithName(font.fontName, font.pointSize, nil)
let fontHeight = CTFontGetDescent(cFont) + CTFontGetAscent(cFont) + CTFontGetLeading(cFont)
sceneParagraphStyle.paragraphSpacingBefore = CGFloat(spacingBefore)
sceneParagraphStyle.paragraphSpacing = fontHeight * CGFloat(spacing)
sceneParagraphStyle.headIndent = ScreenMetrics.pointsFromInches(CGFloat(headIndent))
sceneParagraphStyle.tailIndent = ScreenMetrics.pointsFromInches(CGFloat(tailIndent))
sceneParagraphStyle.firstLineHeadIndent = sceneParagraphStyle.headIndent
sceneParagraphStyle.lineBreakMode = .ByWordWrapping
return sceneParagraphStyle
}
Time Profiler instrument shows a high peak when I press the Tab key. It says that NSTextStorage attributesAtIndex takes up to 40 ms each time I press the Tab key.
I checked: if I remove NSParagraphStyle changes, everything becomes normal. So the question is: how should I update paragraph styles?
Hmm... Didn't found such a solution neither in Apple docs or in Google... Just have experimented and turns out if I add textView.display() after call of self.textStorage.setAttributes, everything works fine!
UPDATE: setNeedsDisplay(invalidRect) does a much better job because you might redraw just a dirty portion of text view's visible rect
I am trying to display an attributed string in a UITextview with clickable links. I've created a simple test project to see where I'm going wrong and still can't figure it out. I've tried enabling user interaction and setting the shouldInteractWithURLs delegate method, but it's still not working. Here's my code (for a view controller that only contains a textview)
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let string = "Google"
let linkString = NSMutableAttributedString(string: string)
linkString.addAttribute(NSLinkAttributeName, value: NSURL(string: "https://www.google.com")!, range: NSMakeRange(0, string.characters.count))
linkString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue", size: 25.0)!, range: NSMakeRange(0, string.characters.count))
textView.attributedText = linkString
textView.delegate = self
textView.selectable = true
textView.userInteractionEnabled = true
}
And here are the delegate methods I've implemented:
func textViewShouldBeginEditing(textView: UITextView) -> Bool {
return false
}
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
return true
}
This still isn't working. I've searched on this topic and nothing has helped yet. Thanks so much in advance.
Just select the UITextView in your storyboard and go to "Show Attributes inspector" and select selectable and links. As the image below shows. Make sure Editable is unchecked.
For swift3.0
override func viewDidLoad() {
super.viewDidLoad()
let linkAttributes = [
NSLinkAttributeName: NSURL(string: "http://stalwartitsolution.co.in/luminutri_flow/terms-condition")!
] as [String : Any]
let attributedString = NSMutableAttributedString(string: "Please tick box to confirm you agree to our Terms & Conditions, Privacy Policy, Disclaimer. ")
attributedString.setAttributes(linkAttributes, range: NSMakeRange(44, 18))
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(value: 1), range: NSMakeRange(44, 18))
textview.delegate = self
textview.attributedText = attributedString
textview.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.red]
textview.textColor = UIColor.white
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
return true
}
Swift 3 iOS 10: Here's Clickable extended UITextView that detect websites inside the textview automatically as long as the link start with www. for example: www.exmaple.com if it exist anywhere in the text will be clickable. Here's the class:
import Foundation
import UIKit
public class ClickableTextView:UITextView{
var tap:UITapGestureRecognizer!
override public init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
print("init")
setup()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup(){
// Add tap gesture recognizer to Text View
tap = UITapGestureRecognizer(target: self, action: #selector(self.myMethodToHandleTap(sender:)))
// tap.delegate = self
self.addGestureRecognizer(tap)
}
func myMethodToHandleTap(sender: UITapGestureRecognizer){
let myTextView = sender.view as! UITextView
let layoutManager = myTextView.layoutManager
// location of tap in myTextView coordinates and taking the inset into account
var location = sender.location(in: myTextView)
location.x -= myTextView.textContainerInset.left;
location.y -= myTextView.textContainerInset.top;
// character index at tap location
let characterIndex = layoutManager.characterIndex(for: location, in: myTextView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
// if index is valid then do something.
if characterIndex < myTextView.textStorage.length {
let orgString = myTextView.attributedText.string
//Find the WWW
var didFind = false
var count:Int = characterIndex
while(count > 2 && didFind == false){
let myRange = NSRange(location: count-1, length: 2)
let substring = (orgString as NSString).substring(with: myRange)
// print(substring,count)
if substring == " w" || (substring == "w." && count == 3){
didFind = true
// print("Did find",count)
var count2 = count
while(count2 < orgString.characters.count){
let myRange = NSRange(location: count2 - 1, length: 2)
let substring = (orgString as NSString).substring(with: myRange)
// print("Did 2",count2,substring)
count2 += 1
//If it was at the end of textView
if count2 == orgString.characters.count {
let length = orgString.characters.count - count
let myRange = NSRange(location: count, length: length)
let substring = (orgString as NSString).substring(with: myRange)
openLink(link: substring)
print("It's a Link",substring)
return
}
//If it's in the middle
if substring.hasSuffix(" "){
let length = count2 - count
let myRange = NSRange(location: count, length: length)
let substring = (orgString as NSString).substring(with: myRange)
openLink(link: substring)
print("It's a Link",substring)
return
}
}
return
}
if substring.hasPrefix(" "){
print("Not a link")
return
}
count -= 1
}
}
}
func openLink(link:String){
if let checkURL = URL(string: "http://\(link.replacingOccurrences(of: " ", with: ""))") {
if UIApplication.shared.canOpenURL(checkURL) {
UIApplication.shared.open(checkURL, options: [:], completionHandler: nil)
print("url successfully opened")
}
} else {
print("invalid url")
}
}
public override func didMoveToWindow() {
if self.window == nil{
self.removeGestureRecognizer(tap)
print("ClickableTextView View removed from")
}
}
}
I'm trying to color all occurrences (not just the first) of a textview string however when I get to the looping I get a cannot invoke rangeOfString argument.
I checked the rangeOfString:options:range documentation on Swift 2 and it looked pretty similar. I'm not really sure what I'm doing wrong.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var textView: UITextView!
func textView(textView: UITextView){
if textView == self.textView {
let nsString = textView.text as NSString
let stringLength = textView.text.characters.count
var range = NSRange(location: 0, length: textView.text.characters.count)
let searchString = textView.text
let text = NSMutableAttributedString(string: textView.text)
text.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, stringLength))
textView.attributedText = text
while(range.location != NSNotFound) {
range = (textView.text as NSString).rangeOfString(searchString, options: NSStringCompareOptions, range: range)
text.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: nsString.rangeOfString("hello"))
}
}
}
}
It looks like you try to highlight user input from search bar. Here how I do so:
func highlightedText(text: NSString, inText: NSString, var withColor: UIColor?) -> NSMutableAttributedString {
var attributedString = NSMutableAttributedString(string:inText as String)
let range = (inText.lowercaseString as NSString).rangeOfString(text.lowercaseString as String)
if withColor == nil {
if let color = globalTint() {
withColor = color
} else {
withColor = UIColor.redColor()
}
}
attributedString.addAttribute(NSForegroundColorAttributeName, value: withColor! , range: range)
return attributedString
}
...
textView.attributedText = highlightedText("string", inText: titleText, color: nil)