What I have is a UILabel (loaded with html) that I put on a UIScrollView.
In the html I have lots of links that I can´t get to "fire-up" when clicked.
I have added UserInteractionEnabled = true both on the UILabel and on the UIScrollview but I can´t get any reaction from the taps on the hyperlinks.
This is the code that create the label with the content.
var attr = new NSAttributedStringDocumentAttributes();
var nsError = new NSError();
attr.DocumentType = NSDocumentType.HTML;
var entry = "some text and url <a href='http://www.google.com'>url</a>";
var myHtmlData = NSData.FromString(entry, NSStringEncoding.Unicode);
contentLabel.AttributedText = new NSAttributedString(myHtmlData, attr, ref nsError);
contentLabel.Frame = new CGRect(0, 20, scrollView.Frame.Size.Width , scrollView.Frame.Size.Height);
contentLabel.LineBreakMode = UILineBreakMode.WordWrap;
contentLabel.Lines = 0;
contentLabel.SizeToFit();
Maybe this is just wrong.. putting html content into a label? But I don´t want to use a webview since it does not seem to scale that well.
Why not using a UITextView instead ? Enable Links detection and disable editable and Selectable option.
Adding HTML to UILabel will not make it interactive.
Use UITextView or https://github.com/TTTAttributedLabel/TTTAttributedLabel
Related
How do I render a UIButton in Xamarin.iOS? See the current Code for the full list.
This is the code I'm using to create and add the button to the Xamarin.Forms.Platform.iOS.CellTableViewCell cell. I cannot get the button to display anything.
With the use of a Foundation.NSMutableAttributedString, it shows a cut-off section of text in the top left corner, regardless of anything I try (alignments, insets, bounds, various constraints, etc). I'm currently trying things from Xamarin.Forms.Platform.iOS.Renderers.ButtonRenderer, but still can't get anything to display at all, no text, no button, or its outline.
If you could fork the repo and fix it or post the solution here, I would be very grateful.
protected override void SetUpContentView()
{
var insets = new UIEdgeInsets(SVConstants.Cell.PADDING.Top.ToNFloat(), SVConstants.Cell.PADDING.Left.ToNFloat(), SVConstants.Cell.PADDING.Bottom.ToNFloat(), SVConstants.Cell.PADDING.Right.ToNFloat());
_Button = new UIButton(UIButtonType.RoundedRect)
{
AutoresizingMask = UIViewAutoresizing.All,
HorizontalAlignment = UIControlContentHorizontalAlignment.Center,
VerticalAlignment = UIControlContentVerticalAlignment.Center,
ContentEdgeInsets = insets,
// TitleEdgeInsets = insets
};
DefaultFontSize = _Button.TitleLabel.ContentScaleFactor;
DefaultTextColor = _Button.TitleLabel.TextColor;
_Recognizer = new UILongPressGestureRecognizer(RunLong);
_Button.TouchUpInside += OnClick; // https://stackoverflow.com/a/51593238/9530917
_Button.AddGestureRecognizer(_Recognizer); // https://stackoverflow.com/a/6179591/9530917
ContentView.AddSubview(_Button);
_Button.CenterXAnchor.ConstraintEqualTo(ContentView.CenterXAnchor).Active = true;
_Button.CenterYAnchor.ConstraintEqualTo(ContentView.CenterYAnchor).Active = true;
_Button.WidthAnchor.ConstraintEqualTo(ContentView.WidthAnchor).Active = true;
_Button.HeightAnchor.ConstraintEqualTo(ContentView.HeightAnchor).Active = true;
UpdateConstraintsIfNeeded();
LayoutIfNeeded();
}
Found out that you can't subclass it. Any button added to the view must be native (UIButton) or custom rendered, such as Xamarin.Forms.Platform.iOS.ButtonRenderer; It doesn't show up otherwise.
EventWebView.frame.size = EventWebView.sizeThatFits(CGSize.zero)
EventWebView.scrollView.isScrollEnabled = false;
var frame = EventWebView.frame;
frame.size.width = EventTableView.frame.size.width-40; // Your desired width here.
frame.size.height = 1; // Set the height to a small one.
EventWebView.frame = frame; // Set webView's Frame, forcing the Layout of its embedded scrollView with current Frame's constraints (Width set above).
frame.size.height = EventWebView.scrollView.contentSize.height; // Get the corresponding height from the webView's embedded scrollView.
heightConstraint.constant = frame.size.height
My HTML Content is "
Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester. Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester.Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester.Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester.Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester.Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester.\n"
Add WebViewDelegate
func webViewDidFinishLoad(_ webView: UIWebView) {
webView.frame.size = webView.sizeThatFits(CGSize.zero)
webView.scrollView.isScrollEnabled = false; // Property available in iOS 5.0 and later
var frame = webView.frame;
frame.size.width = self.view.frame.width-20; // Your desired width here.
frame.size.height = 1; // Set the height to a small one.
webView.frame = frame; // Set webView's Frame, forcing the Layout of its embedded scrollView with current Frame's constraints (Width set above).
frame.size.height = webView.scrollView.contentSize.height; // Get the corresponding height from the webView's embedded scrollView.
webView.frame = frame;
fl = webView.frame.size.height
}
Make sure please check you autolayout constraint also. This method working fine. if your auto layout are not set properly then you can get extra length in it.
Try this
NSString *contentHeight = [webView stringByEvaluatingJavaScriptFromString:#"document.body.offsetHeight;"];
int height = [contentHeight integerValue];
in your webViewDidFinishLoad method. Make sure you set delegate for web view and implement the above delegate methods in your view controller.
I am using swiftyjson to grab some json data and display it in a message box UITextView and UILabel. I have a for LOOP but i can not get the contents to loop inside of my UIScrollview.
I am creating a message box that displays username, date and message inside of a UIView.
My code:
for (_, subJson) in json["messageData"] {
self.outterMessageBox.hidden = false
self.usernameBox.text = subJson["user"].string
self.messageTXTBOX?.text = subJson["message"].string
self.dateBox.text = subJson["datetime"].string
}
And this is the output. Looks fine, just will not loop inside of my scroll view. I've also tried to programmatically create these views with the below code... It still does not loop. What can I be doing wrong?
Programmatically Created Views (i've displays only one iteration of the message box and does not loop as expected).
let myField: UITextField = UITextField (frame: self.ms.bounds)
myField.text = subJson["message"].string! + "\n"
self.ms.addSubview(myField)
let myNameLabel: UILabel = UILabel (frame: self.nm.bounds);
myNameLabel.text = subJson["user"].string
self.nm.addSubview(myNameLabel)
let myDt: UILabel = UILabel (frame: self.dt.bounds)
myDt.text = subJson["datetime"].string
self.dt.addSubview(myDt)
Any help with this would be greatly appreciated.
I'm trying to make my app so that long text posts you don't have to scroll through but have an option to show all and show less (show less is default). I've been trying to do it but I'm not able to do it without changing all of the cells to "Show more" or "Show less". Here is how I make it initially show less by how many characters it has in it (Message = the post text, postCellObj = the object I use to call the cell class) this code is done in cellForRowAtIndexPath...
if messages.isEmpty == false {
var messageString = messages[indexPath.row] as String
var messageFinal = ""
if count(messageString) >= 200 {
var messageNs = messageString as NSString
var messageFinal = messageNs.substringWithRange(NSRange(location: 0, length: 200))
postCellObj.message.text = messageFinal as String + "..."
} else {
postCellObj.message.text = messages[indexPath.row]
}
}
If anyone knows how to do it, it would be much appreciated. Thanks for reading!
I will use two labels for this design. The first label is for your original text, and the other one will show "Show All" or "Show Less".
The only one thing you have to do is to set the frame of the first label (original text).
The frame height for "Show All" could be calculated by line height (firstLabel.font.lineHeight)* number_of_lines_you_want. Remember to set the label property "numberOfLines" and "lineBreakMode" to "ByTruncatingTail".
The frame height for "Show Less" could be calculated by "sizeThatFits", which is supported by UILabel. The parameter "size" could be set to (frame.size.width, MAXFLOAT);
I don't familiar with swift, so I just describe what I will do with this design. Hope this helps!
Hi I want to display some text in my UILabel line by line when an action is occurred
Hi, I have a textbox, button and a UILabel, lets say I've typed in
"This text will be displayed line by line in a UILabel"
and pressed the button to display the text in the UILabel..
so inside the UILabel it will look like:
This
text
will
be
displayed
line
by
line
in
a
UILabel
....
I've been searching forums but could not found an answer.. so I don't know if this is possible.. please help
p.s I've set number of lines to 0 and line breaks is set to word wrap
let label = UILabel(frame: CGRect(x: 100, y: 100, width: 100, height: 300)) // make the height biggest
// label.lineBreakMode = NSLineBreakMode.ByWordWrapping
// label.numberOfLines = 0
dont work if label width bigger word width
var string = "This text will be displayed line by line in a UILabel"
var array = string.componentsSeparatedByString(" ")
var newstring = join("\r", array)
label.text = newstring
let labelText = "Write\rthe\rtext\rwith\rline\rbreaks."
In order to wrap your text within a UILabel on Xcode 6 and above
Select the label you are working with.
Navigate to the Utilities panel.
Select the "Attributes inspector"
Select the number of lines you would like to display on the label (2,3,4) depending on the size of your UILabel field
Select "Word Wrap" under Line Breaks, or you can select any option of your preference.
You may manipulate the way your text appears to your taste.
Does it have to be a UILabel?
You could use UITextField instead which has some advantageous handling options