简体   繁体   English

xamarin ios c#强调UIButton文本

[英]xamarin ios c# underline UIButton text

I am trying to underline the text in this UIButton ("Privacy Policy") . 我试图强调这个UIButton (“隐私政策”)中的文字。 How can this be accomplished? 如何实现这一目标? Here is my code: 这是我的代码:

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);

Also, I found this to work as well. 此外,我发现这也可以。 Thank you! 谢谢!

        //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);

Please set attibuted title on button like this 请在这样的按钮上设置attibuted标题

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


PrivacyPolicy.setAttributedTitle(attStr, forState: .Normal)

And you can't create button using new keyword or you created in that way in swift, So please use like this 你不能使用new关键字创建按钮,或者你在swift中以这种方式创建,所以请使用这样的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM