Xamarin iOS C#下划线UIButton文本

Dou*_*ill 6 c# uibutton visual-studio ios xamarin

我试图在此文本下划线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);
Run Code Online (Sandbox Code Playgroud)

Dou*_*ill 5

另外,我发现它也能正常工作。谢谢!

        //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);
Run Code Online (Sandbox Code Playgroud)


Dha*_*ngh 0

请像这样在按钮上设置属性标题

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


PrivacyPolicy.setAttributedTitle(attStr, forState: .Normal)
Run Code Online (Sandbox Code Playgroud)

并且您无法使用 new 关键字创建按钮或以这种方式快速创建按钮,所以请像这样使用

var PrivacyPolicy = UIButton(frame:CGRect(x: 10,  y: 40, width: 50 , height: 30));
Run Code Online (Sandbox Code Playgroud)