How can I scroll the text on image in xamarin.ios - ios

hear is my code:
var label = new UILabel { Text = text };
label.SetNeedsLayout();
float width = 125;
SizeF size = (SizeF)(label.Text).StringSize(label.Font, new SizeF(width, 100),UILineBreakMode.TailTruncation);
label.Lines = 5;
label.Frame = new RectangleF(20, 40, size.Width, size.Height);
var image = new UIImageView();
image.Image = new UIImage("bluesticky.png");
image.Frame = new CGRect(0, 0, 180, 220);
this.AddSubview(image);
this.AddSubview(label);
In my code UILabel text is placed on UIImage. If the text is larger than the image I want to scroll the text on image.How can I get like that If I have large amount of text. Thanks in advance

its difficult with UIlable - You can do but you will also need scrollview.
Use UITextView instead. Change background color to clear editable property to false. It will automatically manage scroll.
///Update:
To disable dragging of image when Your textView is scrolling, you can disable Dragging event of UIImageview when scroll started and Enable again when scroll ended.
mytextview1.DecelerationStarted += (sender, e) =>
{
//Disable dragging of Image.
};
mytextview1.DecelerationEnded += (sender, e) =>
{
//Enable dragging of Image.
};

Related

Rounded UIButton with size that fits its title

I'd like to have a rounded UIButton whose size will be constrained by its title.
This button will be placed in a Horizontal stack view, with a preceding text. I want the size of the button to be fixed while the size of the preceding text is only constrained by the space available alongside the button.
Here is the code I have so far:
UIStackView textAndDoneButtonStackView = new UIStackView
{
TranslatesAutoresizingMaskIntoConstraints = false,
Axis = UILayoutConstraintAxis.Horizontal,
Distribution = UIStackViewDistribution.Fill,
Alignment = UIStackViewAlignment.Center,
Spacing = 10
};
// Create the appropriate control for this habit
UIView habitTitleView = CreateControlForHabit(habitTitle, kv.Value.workout, workoutName);
textAndDoneButtonStackView.AddArrangedSubview(habitTitleView);
// Add the done button
UIButton doneButton = CreateDoneButton(healthy_green);
habitIdDoneButtonId[kv.Value.id] = (int)doneButton.Tag;
textAndDoneButtonStackView.AddArrangedSubview(doneButton);
and
CreateControlForHabit(...)
{
UILabel uIHabitTitle = new UILabel { TranslatesAutoresizingMaskIntoConstraints = false };
uIHabitTitle.Text = habitTitle;
return uIHabitTitle;
}
then
private static UIButton CreateDoneButton(UIColor healthy_green)
{
// Add the done button
var doneButton = UIButton.FromType(UIButtonType.RoundedRect);
doneButton.TranslatesAutoresizingMaskIntoConstraints = false;
doneButton.SetTitle(Utilities.GetLocalizedString("habit_done_button_text"), UIControlState.Normal);
doneButton.SetTitleColor(UIColor.White, UIControlState.Normal);
doneButton.BackgroundColor = healthy_green; // Healthy Green
doneButton.Layer.CornerRadius = 5f;
doneButton.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
doneButton.ContentEdgeInsets = new UIEdgeInsets(top: 0, left: 10f, bottom: 0, right: 10f);
doneButton.Tag = doneButton.GenerateViewTag();
// View is hidden by default
doneButton.Hidden = true;
return doneButton;
}
My problem is that I want a rounded button and I can't cast the result of UIButton.FromType() to a custom class that would override IntrinsicContentSize.
What are the options available here to fix the size of this rounded button?

Title of UIButton is not visible when UIVibrancyEffect is applied

I'm having trouble applying UIVibrancyEffect to my UIButtons in iOS today widget. I want them to like default "Edit" button in notification centre's Today section:
As you can see in the screenshot, default button is vibrant and looks much better.
I tried replacing widget's View with UIVisualEffectView like that:
UIVisualEffectView effectView = new UIVisualEffectView(UIVibrancyEffect.CreateForNotificationCenter ());
effectView.Frame = this.View.Bounds;
effectView.AutoresizingMask = this.View.AutoresizingMask;
UIView oldView = this.View;
this.View = effectView;
effectView.ContentView.AddSubview(oldView);
this.View.TintColor = UIColor.Clear;
And it appears to be working, but titles of my buttons become vibrant as well (I want them to remain black):
Is there a way to prevent button titles from becoming vibrant when UIVibrancyEffect is applied?
I should also add that I'm using Xamarin.iOS.
I seem to have a solution to my own problem. I ended up creating UIVisualEffectView with blank UIView inside and adding it behind UIButton.
This code snippet shows how to style one button:
// Create effect view
var effectView = new UIVisualEffectView(UIVibrancyEffect.CreateForNotificationCenter ());
// This color looks best for me. You can play around with it to make it look better
effectView.BackgroundColor = UIColor.FromRGBA(55, 55, 55, 100);
// Position effectView
effectView.Frame = myButton.Frame;
// Add effectView and send it to back, behind actual button
this.View.AddSubview(effectView);
this.View.SendSubviewToBack (effectView);
// Create UIView and add it to effectView's ContentView
var view = new UIView ();
view.BackgroundColor = UIColor.White;
effectView.ContentView.AddSubview(view);
view.Frame = new CGRect(0, 0, effectView.Frame.Width, effectView.Frame.Height);
// Make sure your effect view has rounded corners
effectView.Layer.MasksToBounds = true;
effectView.Layer.CornerRadius = 4.0f;
And this is how it looks:

fading scrollMenu with labels

i've created a paged UIScrollView which contains labels on every page. How can i lower the margin between the labels like in the picture and then still being able to scroll?
At the moment my navigationBar look like following. Where u can swipe to next page to the right and get the next label.
What i want is something like this with a small margin and still being able to swipe.
viewDidLoad code
//Category ScrollView
categoryScrollView = UIScrollView(frame: CGRectMake(0, self.navigationController!.navigationBar.frame.height-38, self.view.frame.width, 38))
categoryScrollView?.contentSize = CGSizeMake(self.view.frame.width, 38)
categoryScrollView?.delegate = self
categoryScrollView?.pagingEnabled = true
self.navigationController?.navigationBar.addSubview(categoryScrollView!)
categoryArray = NSArray(objects: "Book", "Elektronik")
var textWidth = 0
for val in categoryArray!
{
var textLabel: UILabel = UILabel()
textLabel.textAlignment = NSTextAlignment.Center
textLabel.text = val as NSString
textLabel.frame = CGRectMake(CGFloat(textWidth), 0, categoryScrollView!.frame.width, categoryScrollView!.frame.height)
categoryScrollView?.addSubview(textLabel)
textWidth = textWidth + Int(textLabel.frame.size.width)
if textWidth > Int(self.view.frame.width) {
categoryScrollView?.contentSize = CGSizeMake(CGFloat(textWidth), categoryScrollView!.frame.height);
}
}
The default width of the "page" when pagingEnabled is switched on is the width of the scroll view. You could reduce the width and let the contained views be visible beyond the edges of the view by setting clipsToBounds to false.
If this does not work for you there also the option to adjust the paging width by intercepting the end of the drag via the UIScrollViewDelegate method scrollViewWillEndDragging(...) where you can adjust the targetContentOffset to fit your needs (see an example here).

UIScrollView with UITableView not scrollable

My App (Xamarin/monotouch for iPhone) consists of three UITableView which I add to a UIScrollView (all is done programtically)
Each of the tables is not scrollable
myTableView.ScrollEnabled = false;
I calculate the height needed for x number of items and set it in runtime.
myTableView.Frame = new RectangleF (0, 27, 320, myTableViewHeight);
The problem is that the UIScrollView is not scrollable:
int caclulateScrollViewHeight = myListHeight + oListHeight + 104;
scrollView.ContentSize = new SizeF (320, caclulateScrollViewHeight);
scrollView.Frame = new RectangleF (0, 135, 320, caclulateScrollViewHeight);
scrollView.DelaysContentTouches = true;
scrollView.CanCancelContentTouches = false;
this.View.Add (scrollView);
Any idea what can cause that?
Don't set your scrollView frame height the same as your calculated view height. The frame is the box on screen that you want it to occupy; generally you'll want to keep this less than or equal to screen size. If your content size matches your frame size, it won't scroll (because it won't need to).
scrollView.Frame = new RectangleF (0, 135, 320, 200);

Two Views, one appears sometimes and when so, the other one takes it space

I have an image and a label who are are next to each other inside a TableCell (among other elements). Sometimes the image has to be shown, and sometimes not.
What I want is that the label takes all space when the image has to be hidden, and respects its space (so it does not appear over the image) when it has to be shown.
In Android I would not have a problem doing it; I would put those two elements in a LinearLayout and I would mark the imageĀ“s visibility as GONE when I have to hide it, and then the text would start naturally where the image should be. But with the storyboard I am a bit lost and I do not know how to control this wanted behaviour. Is there a similar technique as the one that I mentioned for Android? I have to achieve it without AutoLayout.
I tried to change the origin of the frame of the label when the image is there, but it got all weird when the table downloads new elements:
- (void)displayImage
{
self.image.hidden = !_question.showImage;
// Make a bit extra space for the image
if(_question.showImage){
CGRect labelFrame = [self.unreadLabel frame];
labelFrame.origin.x = labelFrame.origin.x +20;
[self.unreadLabel setFrame:labelFrame];
}
}
Ok, I was not taking into account that the cells are recycled and that I had to set them up also when the text has to take all space.
The resulting method would look like this:
- (void)displayImage
{
self.image.hidden = !_question.image;
// Make a bit extra space for the image
CGRect labelFrame = [self.unreadLabel frame];
CGRect imageFrame = [self.image frame];
if(_question.showimage){
labelFrame.origin.x = imageFrame.origin.x + imageFrame.size.width + 3;
[self.unreadLabel setFrame:labelFrame];
} else{
labelFrame.origin.x = imageFrame.origin.x ;
[self.unreadLabel setFrame:labelFrame];
}
}

Resources