html给标题加下划线,如何在Button中给title添加下划线

本文详细介绍了在iOS开发中如何自定义登录按钮的样式,包括使用NSMutableAttributedString设置下划线样式,如单细线、单粗线等,并通过添加监听事件实现点击交互。同时,解释了NSUnderlineStyle的各个枚举值及其应用场景。
摘要由CSDN通过智能技术生成

设计图如下:

5d1060f1d619

Snip20180719_1.png

1.代码如下:

//登录方式的选择

let selectedLoginBtn: UIButton = {

let button = UIButton()

button.titleLabel?.font = UIFont.systemFont(ofSize: 12.plan_scaleValue)

let attributedStr: NSMutableAttributedString = NSMutableAttributedString.init(string: "账号密码登录")

let titleRange: NSRange = NSRange.init(location: 0, length: attributedStr.length)

let number: NSNumber = NSNumber(integerLiteral: NSUnderlineStyle.styleThick.rawValue)

attributedStr.addAttributes([NSUnderlineStyleAttributeName: number,

NSForegroundColorAttributeName: COLOR_222222,

NSFontAttributeName: UIFont.systemFont(ofSize: 12.plan_scaleValue)],

range: titleRange)

button.setAttributedTitle(attributedStr, for: UIControlState.normal)

button.addTarget(self, action: #selector(selectedLoginAction(_ :)), for: .touchUpInside)

button.isUserInteractionEnabled = true

button.translatesAutoresizingMaskIntoConstraints = false

return button

}()

2.NSUnderlineStyle中的key值对应的解释说明

case styleNone //无下划线

case styleSingle // 有下划线,样式为单细线

@available(iOS 7.0, *)

case styleThick //有下划线,样式为单粗线

@available(iOS 7.0, *)

case styleDouble //有下划线,样式为双细线

@available(iOS 7.0, *)

public static var patternSolid: NSUnderlineStyle { get } //有下划线,样式为连续实现

@available(iOS 7.0, *)

case patternDot //有下划线,样式为虚线 比如这样:------

@available(iOS 7.0, *)

case patternDash //有下划线,样式为破折线 比如这样:—— —— ——

@available(iOS 7.0, *)

case patternDashDot //有下划线,样式为连续的点和破折线 比如这样:——-——-——-

@available(iOS 7.0, *)

case patternDashDotDot //有下划线,样式为连续的破折号、点、点 比如这样:——-——-——-

@available(iOS 7.0, *)

case byWord //在有空格的地方不设置下划线/删除线

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值