ios NS下划线样式属性名称下划线间距

x33g5p2x  于 2023-01-06  发布在  iOS
关注(0)|答案(4)|浏览(355)

我希望下划线像其他普通下划线机制一样位于文本下方。但是,使用NSAttributed string时,它会留下"g"和"y"的漏洞。
示例:

它的外观应该是:

如何增加下划线和标签之间的间距?

p4tfgftt

p4tfgftt1#

使用NSAttributedString或CoreText无法控制这种行为(除了自己画下划线之外),NSAttributedString也没有这种选项(CoreText hasn't got one, either也是如此)。
在苹果系统上,第一个版本(差距)是“预期”行为,因为它是苹果提供的,并在整个系统(以及Safari、TextEdit等应用程序)中使用。
如果你真的,真的想让下划线没有间隙,你需要画一个没有下划线的字符串,然后自己画线(我需要做in one of my projects and I can tell you it's hard;参见this file,搜索“下划线”)。

w6mmgewl

w6mmgewl2#

我添加了一条线(UIView),高度为1,宽度与label相同,与UILabel的底部对齐。

let label = UILabel()
    label.text = "underlined text"

    let spacing = 2 // will be added as negative bottom margin for more spacing between label and line

    let line = UIView()
    line.translatesAutoresizingMaskIntoConstraints = false
    line.backgroundColor = label.textColor
    label.addSubview(line)
    label.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[line]|", metrics: nil, views: ["line":line]))
    label.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[line(1)]-(\(-spacing))-|", metrics: nil, views: ["line":line]))
dsekswqp

dsekswqp3#

您可以使用UITextView I在NSLayoutManager中添加自定义NSAttributedStringKey“customUnderline”和swizzling方法drawUnderline。

import Foundation
import SwiftyAttributes
import UIKit

private let swizzling: (AnyClass, Selector, Selector) -> Void = { forClass, originalSelector, swizzledSelector in
    guard let originalMethod = class_getInstanceMethod(forClass, originalSelector),
        let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector) else {
            return
    }
    method_exchangeImplementations(originalMethod, swizzledMethod)
}

extension NSAttributedStringKey {
    static var customUnderline: NSAttributedStringKey = NSAttributedStringKey("customUnderline")
}

extension Attribute {
    static var customUnderline: Attribute = Attribute.custom(NSAttributedStringKey.customUnderline.rawValue, true)
}

extension NSLayoutManager {

    // MARK: - Properties

    static let initSwizzling: Void = {
        let originalSelector = #selector(drawUnderline(forGlyphRange:underlineType:baselineOffset:lineFragmentRect:lineFragmentGlyphRange:containerOrigin:))
        let swizzledSelector = #selector(swizzled_drawUnderline(forGlyphRange:underlineType:baselineOffset:lineFragmentRect:lineFragmentGlyphRange:containerOrigin:))
        swizzling(NSLayoutManager.self, originalSelector, swizzledSelector)
    }()

    // MARK: - Functions

    @objc
    func swizzled_drawUnderline(forGlyphRange glyphRange: NSRange, underlineType underlineVal: NSUnderlineStyle, baselineOffset: CGFloat, lineFragmentRect lineRect: CGRect, lineFragmentGlyphRange lineGlyphRange: NSRange, containerOrigin: CGPoint) {
        guard needCustomizeUnderline(underlineType: underlineVal) else {
            swizzled_drawUnderline(forGlyphRange: glyphRange,
                                   underlineType: underlineVal,
                                   baselineOffset: baselineOffset,
                                   lineFragmentRect: lineRect,
                                   lineFragmentGlyphRange: lineGlyphRange,
                                   containerOrigin: containerOrigin)
            return
        }

        let heightOffset = containerOrigin.y - 1 + (getFontHeight(in: glyphRange) ?? (lineRect.height / 2))
        drawStrikethrough(forGlyphRange: glyphRange,
                          strikethroughType: underlineVal,
                          baselineOffset: baselineOffset,
                          lineFragmentRect: lineRect,
                          lineFragmentGlyphRange: lineGlyphRange,
                          containerOrigin: CGPoint(x: containerOrigin.x,
                                                   y: heightOffset))
    }

    // MARK: - Private functions

    private func needCustomizeUnderline(underlineType underlineVal: NSUnderlineStyle) -> Bool {
        guard underlineVal == NSUnderlineStyle.styleSingle else {
            return false
        }
        let attributes = textStorage?.attributes(at: 0, effectiveRange: nil)
        guard let isCustomUnderline = attributes?.keys.contains(.customUnderline), isCustomUnderline else {
            return false
        }
        return true
    }

    private func getFontHeight(in glyphRange: NSRange) -> CGFloat? {
        let location = characterRange(forGlyphRange: glyphRange, actualGlyphRange: nil).location
        guard let font = textStorage?.attribute(.font, at: location, effectiveRange: nil) as? UIFont else {
            return nil
        }
        return font.capHeight
    }
}

like that

nue99wik

nue99wik4#

费兰·梅林奇的回答对我最有效。
我把它变成了一个UILabel扩展,我想我会分享它。

extension UILabel {

func addUnbrokenUnderline() {
    let line = UIView()
    line.translatesAutoresizingMaskIntoConstraints = false
    line.backgroundColor = textColor
    addSubview(line)
    NSLayoutConstraint.activate([
        line.heightAnchor.constraint(equalToConstant: 1.0),
        line.leadingAnchor.constraint(equalTo: leadingAnchor),
        line.widthAnchor.constraint(equalToConstant: intrinsicContentSize.width),
        line.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -5.0)
    ])
}

}

相关问题