【发布时间】:2019-10-15 12:38:06
【问题描述】:
我知道UITextFields可以使用SecureTextEntry属性实现密码风格的效果,我也在网上找到了一些代码来实现隐藏/显示密码效果here,但这仅适用于@987654326 @,我需要为自定义 UITextView 实现相同的功能。我目前有一些代码来实现为UI添加图像,但没有实现实际的显示/隐藏密码效果。
我在Swifthere 中找到了一些关于如何执行此操作的代码,但我从未与Swift 合作过,希望熟悉Swift 的人可以为我将其翻译成C# ,因为这可能是我需要的解决方案。
我也明白,虽然SecureTextEntry 属性在设置为 true 时为UITextFields 提供密码风格的体验(即防止复制并将字符变成黑点),它仅在为 UITextViews 设置为 true 时防止复制文本。这是我从这个属性的文档中找到的 here
这是我目前在文件中实现显示/隐藏密码效果的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ResolutionGroupName("Xamarin")]
[assembly:ExportEffect(typeof(MyApp.iOS.CustomRenderers.PasswordEffect), "PasswordEffect")]
namespace MyApp.iOS.CustomRenderers
{
public class PasswordEffect : PlatformEffect
{
protected override void OnAttached()
{
Configure();
}
protected override void OnDetached()
{
//do nothing
}
private void Configure()
{
if (Control != null)
{
if (Control is UITextView) {
UITextView vUpdatedEntry = (UITextView)Control;
var buttonRect = UIButton.FromType(UIButtonType.Custom);
buttonRect.SetImage(UIImage.FromBundle("eye_image"), UIControlState.Normal);
buttonRect.TouchUpInside += (object sender, EventArgs e1) => {
if (vUpdatedEntry.SecureTextEntry)
{
vUpdatedEntry.SecureTextEntry = false;
buttonRect.SetImage(UIImage.FromBundle("eye_crossed_image"), UIControlState.Normal);
}
else
{
vUpdatedEntry.SecureTextEntry = true;
buttonRect.SetImage(UIImage.FromBundle("eye_image"), UIControlState.Normal);
}
};
// Would love to have password effect here :)
vUpdatedEntry.ShouldChangeText += (textField, range, replacementString) => {
string text = vUpdatedEntry.Text;
var result = text.Substring(0, (int)range.Location) + replacementString + text.Substring((int)range.Location + (int)range.Length);
vUpdatedEntry.Text = result;
return false;
};
buttonRect.Frame = new CoreGraphics.CGRect(10.0f, 0.0f, 15.0f, 15.0f);
buttonRect.ContentMode = UIViewContentMode.ScaleToFill;
UIView paddingViewRight = new UIView(new System.Drawing.RectangleF(0.0f, 0.0f, 30.0f, 18.0f));
paddingViewRight.AddSubview(buttonRect);
buttonRect.TranslatesAutoresizingMaskIntoConstraints = false;
buttonRect.CenterYAnchor.ConstraintEqualTo(paddingViewRight.CenterYAnchor).Active = true;
vUpdatedEntry.TextContainerInset = new UIEdgeInsets(8.0f, 0.0f, 8.0f, paddingViewRight.Frame.Width+5.0f);
vUpdatedEntry.AddSubview(paddingViewRight);
paddingViewRight.TranslatesAutoresizingMaskIntoConstraints = false;
paddingViewRight.TrailingAnchor.ConstraintEqualTo(vUpdatedEntry.LayoutMarginsGuide.TrailingAnchor, 9.0f).Active = true;
paddingViewRight.HeightAnchor.ConstraintEqualTo(vUpdatedEntry.HeightAnchor).Active = true;
paddingViewRight.WidthAnchor.ConstraintEqualTo(buttonRect.WidthAnchor,1.0f, 0.0f).Active = true;
Control.Layer.CornerRadius = 4;
Control.Layer.BorderColor = new CoreGraphics.CGColor(255, 255, 255);
Control.Layer.MasksToBounds = true;
vUpdatedEntry.TextAlignment = UITextAlignment.Left;
}
}
}
}
}
【问题讨论】:
标签: c# ios swift xamarin xamarin.ios