optonaut / ActiveLabel.swift

UILabel drop-in replacement supporting Hashtags (#), Mentions (@) and URLs (http://) written in Swift
MIT License
4.47k stars 686 forks source link

Underline text does not work for swift 4? #220

Closed dhirajdebut closed 6 years ago

dhirajdebut commented 6 years ago

Hi,

I am trying to underline particular words in a string. I have tried below code but it does not work.

 label.configureLinkAttribute = { (type, attributes, isSelected) in
                var atts = attributes
                switch type {
                case customType1:
                atts[NSAttributedStringKey.font._rawValue as String] = UIFont(name: self.labelTc.font.fontName, size: 15.0)
               atts[NSAttributedStringKey.underlineStyle.rawValue] = NSUnderlineStyle.styleSingle
                break
                case .mention:
                    break
                case .hashtag:
                    break
                case .url:
                    break
                case .custom(let pattern):
                    break

                default :
                    break
                }

                return atts
            }

> 

> 

> 
phngoctam commented 6 years ago

hi @dhirajdebut , you should use this atts[NSAttributedStringKey.underlineStyle] = NSUnderlineStyle.styleSingle.rawValue instead of atts[NSAttributedStringKey.underlineStyle.rawValue] = NSUnderlineStyle.styleSingle Hope this help.

OHeroJ commented 6 years ago

0.8.1 crash

label.configureLinkAttribute = { (type, attributes, isSelected) in
                var retDic = attributes
                switch type {
                case customType:
                 retDic[NSAttributedStringKey.underlineStyle] = NSUnderlineStyle.styleThick                 
                let paragraph = NSMutableParagraphStyle()
                paragraph.lineSpacing = 5
                retDic[NSAttributedStringKey.paragraphStyle] = paragraph 
                }
                return retDic
            }

crash!!

but if you

label.configureLinkAttribute = { (type, attributes, isSelected) in
                var retDic = attributes
                switch type {
                case customType:
//                    retDic[NSAttributedStringKey.underlineStyle] = NSUnderlineStyle.styleThick
//                    let paragraph = NSMutableParagraphStyle()
//                    paragraph.lineSpacing = 5
//                    retDic[NSAttributedStringKey.paragraphStyle] = paragraph
                    retDic[NSAttributedStringKey.font] = UIFont.mg.m14
                default: break
                }
                return retDic
            }

run well

piemonte commented 6 years ago

i've encountered this as well but haven't had a chance to look into it. the underline renders after there's interaction, so it works! but i believe there just needs to be a style check or layout performed after initialization and link attribution is configured.

piemonte commented 6 years ago
    fileprivate lazy var _label: ActiveLabel = {
        let label = ActiveLabel()
        let labelText = "hello"
        let helloType = ActiveType.custom(pattern: labelText)
        label.handleCustomTap(for: helloType, handler: { (string) in
             // action
        })
        label.numberOfLines = 3
        label.textAlignment = .center
        label.textColor = UIColor.white
        label.customColor = [helloType : UIColor.orange]
        label.enabledTypes = [helloType]
        label.text = "hello is an underlined link"
        label.configureLinkAttribute = { (type, attributes, isSelected) in
            var atts = attributes
            atts[NSAttributedStringKey.underlineStyle] = NSUnderlineStyle.styleThick.rawValue
            return atts
        }
        return label
    }()
piemonte commented 6 years ago

solution: if you set the text following configureLinkAttribute, then it works.

maziyarpanahi commented 6 years ago

Feel free to re-open this issue.