xamarin ios c# underline UIButton text

本文关键字:UIButton text underline ios xamarin | 更新日期: 2023-09-27 18:25:09

我正试图在此UIButton("隐私政策")中给文本加下划线。如何才能做到这一点?这是我的代码:

var PrivacyPolicy = new UIButton(new CGRect(10, s.MxHt - 40, (s.MxWd - 20) / 3, 30));
PrivacyPolicy.BackgroundColor = UIColor.FromRGB(0, 92, 191);
PrivacyPolicy.Font = UIFont.FromName("Roboto", 14f);
PrivacyPolicy.SetTitle("Privacy Policy", UIControlState.Normal);
PrivacyPolicy.TouchUpInside += HandleBtnOpenPrivacyTouchUpInside;            
if (iPad == false)
{
    PrivacyPolicy.Font = UIFont.SystemFontOfSize(12);
}
View.AddSubview(PrivacyPolicy);

xamarin ios c# underline UIButton text

此外,我发现这也很有效。非常感谢。

        //Set attribute for underlineing information links
        var underlineAttr = new UIStringAttributes { UnderlineStyle = NSUnderlineStyle.Single, ForegroundColor = UIColor.White };
        //Privacy Button Link
        var PrivacyPolicy = new UIButton(new CGRect(10, s.MxHt - 40, (s.MxWd - 20) / 3, 30));
        PrivacyPolicy.BackgroundColor = UIColor.FromRGB(0, 92, 191);
        PrivacyPolicy.Font = UIFont.FromName("Roboto", 14f);            
        PrivacyPolicy.SetAttributedTitle(new NSAttributedString("Privacy Policy", underlineAttr), UIControlState.Normal);
        PrivacyPolicy.TouchUpInside += HandleBtnOpenPrivacyTouchUpInside;            
        if (iPad == false) { PrivacyPolicy.Font = UIFont.SystemFontOfSize(12); }            
        View.AddSubview(PrivacyPolicy);

请在类似的按钮上设置分配的标题

let attStr : NSMutableAttributedString = NSMutableAttributedString(string: "Privacy Policy")
        attStr.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: NSMakeRange(0, attStr.length))

PrivacyPolicy.setAttributedTitle(attStr, forState: .Normal)

你不能使用新的关键字创建按钮,或者你在swift中以这种方式创建按钮,所以请像这样使用

var PrivacyPolicy = UIButton(frame:CGRect(x: 10,  y: 40, width: 50 , height: 30));